用
HorizontalScrollView 和GridView 实现横向滑动时用几个地方需要注意的,这里记录一下,以便后续参考.
1.GridView 显示大小异常.
不管在布局中如何设置宽高,会发现都没有效果,这个时候要把HorizontalScrollView 的布局中增加
android:fillViewport="true"
2.不可以使用HorizontalScrollView直接嵌套GridView,需要有Layout包裹
然后设置Gridview的layout_width 一个具体的宽度.一个可以超出屏幕最大宽度的值,可以在布局中设置也可以在java 代码中设置.
如果在代码中手动设置GridView 的宽度是,需要注意的是设置
setLayoutParams 的时候,参数的类型要和其parent 的layout 类型一致,比如GridView的parent 是RelativeLayout 那
GridView 的
setLayoutParams
就要是
RelativeLayout.LayoutParams 类型的.
上面说了几个需要注意的地方,下面贴上相关的代码.
HorizontalscrollActivity .java
public class HorizontalscrollActivity extends Activity {private List<HashMap<String, Object>> list ;private final int DATA_SIZE = 8;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_horizontal_scroll);DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);float density = dm.density;int ll_width = (int) ((100+5) * density * DATA_SIZE);RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ll_width, RelativeLayout.LayoutParams.WRAP_CONTENT);GridView gv = (GridView)this.findViewById(R.id.gv);gv.setLayoutParams(params);gv.setNumColumns(DATA_SIZE);getData();ListAdapter adapter = new SimpleAdapter(this, list, R.layout.gridview_item,new String[]{"itemImage","itemName"},new int[]{R.id.itemImage,R.id.itemName});gv.setAdapter(adapter);}private void getData() {list = new ArrayList<HashMap<String,Object>>();for (int i = 0; i < DATA_SIZE; i++) {HashMap<String, Object> map = new HashMap<String, Object>();map.put("itemImage", R.drawable.ic_launcher);map.put("itemName", "图片-" + i);list.add(map);}}}
activity_horizontal_scroll.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><HorizontalScrollViewandroid:id="@+id/horizontalscrollview"android:layout_width="match_parent"android:layout_height="wrap_content"android:scrollbars="none"android:fillViewport="true"><RelativeLayout android:id="@+id/ll_horizontalscrollview"android:layout_width="match_parent"android:layout_height="wrap_content"><GridViewandroid:id="@+id/gv"android:layout_width="wrap_content"android:layout_height="match_parent"android:numColumns="auto_fit"android:horizontalSpacing="10dp"/></RelativeLayout></HorizontalScrollView></LinearLayout>
gridview_item.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content" ><ImageViewandroid:id="@+id/itemImage"android:layout_width="100dp"android:layout_height="100dp"android:layout_centerHorizontal="true" ></ImageView><TextViewandroid:id="@+id/itemName"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/itemImage"android:layout_centerHorizontal="true" ></TextView></RelativeLayout>
横滑GridView实践
589

被折叠的 条评论
为什么被折叠?



