HorizontalScrollView 和GridView 实现横向滑动

用  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
   
   
  1. public class HorizontalscrollActivity extends Activity {
  2. private List<HashMap<String, Object>> list ;
  3. private final int DATA_SIZE = 8;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_horizontal_scroll);
  8. DisplayMetrics dm = new DisplayMetrics();
  9. getWindowManager().getDefaultDisplay().getMetrics(dm);
  10. float density = dm.density;
  11. int ll_width = (int) ((100+5) * density * DATA_SIZE);
  12. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ll_width, RelativeLayout.LayoutParams.WRAP_CONTENT);
  13. GridView gv = (GridView)this.findViewById(R.id.gv);
  14. gv.setLayoutParams(params);
  15. gv.setNumColumns(DATA_SIZE);
  16. getData();
  17. ListAdapter adapter = new SimpleAdapter(this, list, R.layout.gridview_item,
  18. new String[]{"itemImage","itemName"},
  19. new int[]{R.id.itemImage,R.id.itemName});
  20. gv.setAdapter(adapter);
  21. }
  22. private void getData() {
  23. list = new ArrayList<HashMap<String,Object>>();
  24. for (int i = 0; i < DATA_SIZE; i++) {
  25. HashMap<String, Object> map = new HashMap<String, Object>();
  26. map.put("itemImage", R.drawable.ic_launcher);
  27. map.put("itemName", "图片-" + i);
  28. list.add(map);
  29. }
  30. }
  31. }

activity_horizontal_scroll.xml
   
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <HorizontalScrollView
  7. android:id="@+id/horizontalscrollview"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:scrollbars="none"
  11. android:fillViewport="true">
  12. <RelativeLayout android:id="@+id/ll_horizontalscrollview"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content">
  15. <GridView
  16. android:id="@+id/gv"
  17. android:layout_width="wrap_content"
  18. android:layout_height="match_parent"
  19. android:numColumns="auto_fit"
  20. android:horizontalSpacing="10dp"
  21. />
  22. </RelativeLayout>
  23. </HorizontalScrollView>
  24. </LinearLayout>

gridview_item.xml
   
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content" >
  5. <ImageView
  6. android:id="@+id/itemImage"
  7. android:layout_width="100dp"
  8. android:layout_height="100dp"
  9. android:layout_centerHorizontal="true" >
  10. </ImageView>
  11. <TextView
  12. android:id="@+id/itemName"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_below="@+id/itemImage"
  16. android:layout_centerHorizontal="true" >
  17. </TextView>
  18. </RelativeLayout>














 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值