HorizontalScrollView 和GridView 实现横向滑动
2017-02-14 15:32
525 查看
用 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
activity_horizontal_scroll.xml
gridview_item.xml
实现横向滑动时用几个地方需要注意的,这里记录一下,以便后续参考.
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;
@Override
protected 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" >
<HorizontalScrollView
android: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">
<GridView
android: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" >
<ImageView
android:id="@+id/itemImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true" >
</ImageView>
<TextView
android:id="@+id/itemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/itemImage"
android:layout_centerHorizontal="true" >
</TextView>
</RelativeLayout>
相关文章推荐
- go-操作oracle数据库
- 润乾自定义数据集发布报表报空指针异常
- Js数组的基本方法2
- Android黑名单自动挂断来电(AIDL)
- 优化Mysql数据库的方法
- Redis 如何存储一条用户信息
- 使用ssh无法连接虚拟机linux系统
- 算法导论 红黑树 学习 旋转(二)
- Python面试必须要看的15个问题
- redhat6.5安装httpd 2.2.27并开启server-status
- 《Go学习笔记 . 雨痕》流程控制(if、switch、for range、goto、continue、break)
- 企业为什么要去美国建厂
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- 11 container with most water
- Python如何解决Cx_Oracle查询时UnicodeDecodeError的问题?
- 北京理工大学2001年上机题目之四
- python 建立 C++类和单例类脚本
- tableView 数据源修改后,reloaddata数据没有更新
- 支付宝上天了! 移动支付应用首次登上飞机
- 90.Spring Boot 1.4 使用log4j错误【从零开始学Spring Boot】