Android ScrollView中嵌套GridView,ListView
2016-06-30 15:03
344 查看
最近开发中遇到一个问题,就是在ScrollView中嵌套GridView,ListView加载数据时,不管怎样都只显示一条数据。其它的数据都没出来。查询资料后来找到了完美的解决办法:
1:重写GridView,ListView
GridView的重写:
ListView的重写:
2:获得足够高度
上面方法的使用:
1:重写GridView,ListView
GridView的重写:
public class MyGridView extends GridView { public MyGridView(Context context, AttributeSet attrs) { super(context, attrs); } public MyGridView(Context context) { super(context); } public MyGridView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
ListView的重写:
public class MyListView extends ListView { public MyListView(Context context) { // TODO Auto-generated method stub super(context); } public MyListView(Context context, AttributeSet attrs) { // TODO Auto-generated method stub super(context, attrs); } public MyListView(Context context, AttributeSet attrs, int defStyle) { // TODO Auto-generated method stub super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
2:获得足够高度
public void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); ((MarginLayoutParams)params).setMargins(15, 15, 15, 15); listView.setLayoutParams(params); }
上面方法的使用:
ListView list = findViewById(R.id.listView); setListViewHeightBasedOnChildren(list);
相关文章推荐
- 【Android】如何通知MediaScanner扫描出指定文件
- android客户端更新常规流程
- Android瀑布流照片墙实现,体验不规则排列的美感
- Android 事件总线Otto框架详解二
- android EventBus 学习(二)
- Android5.0 拦截第三方推送的通知消息
- Android Design Support Library 中控件的使用简单介绍(一)
- android-Paint
- 15 个 Android 通用流行框架大全 2016
- Android studio gradle构建工程的时候很慢经常报错解决方案
- 从Android代码中来记忆23种设计模式
- Android bug——Launcher 0x506导致花屏问题
- Android应用开发学习—Toast使用方法
- Android通用流行框架大全
- 如何使用Cordova获取Android的唯一标识符
- AndroidStudio 快捷键
- Android Studio系列教程3 (快捷键)
- 本文转载Doctorq原创文章: Android Framework中的Application Framework层介绍
- Android Studio如何发布APK
- android常犯错误记录(一)