ScrollView嵌套listView,嵌套Gridview显示不全的解决办法
2016-12-16 15:22
489 查看
ScrolView里面嵌套了listview或者ScrollView里面嵌套GridView显示不全,只显示一条的高度,是不是很揪心,不要揪心啦,大不了不嵌套嘛,怎么能...怎么能???珍惜生命,哈哈... 方案1,自定义ListView,使用的时候用这个SuperListView就可以了.
public class SuperListView extends ListView { public SuperListView(Context context) { super(context); } public SuperListView(Context context, AttributeSet attrs) { super(context, attrs); } public SuperListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
方案2. 重新计算嵌套内容的高度
比如我在ScrollView里嵌套了一个ImageView和一个ListView,那么就可以调用以下方法:
public void setListViewHeightBasedOnChildren(ListView listView, ImageView iv_test) { // 获取ListView对应的Adapter ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0, len = listAdapter.getCount(); i < len; i++) { View listItem = listAdapter.getView(i, null, listView);// listAdapter.getCount()返回数据项的数目 listItem.measure(0, 0);// 计算子项View 的宽高 totalHeight += listItem.getMeasuredHeight();// 统计所有子项的总高度 } totalHeight = totalHeight + iv_test.getHeight() + 5; ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); // listView.getDividerHeight()获取子项间分隔符占用的高度 // params.height最后得到整个ListView完整显示需要的高度 listView.setLayoutParams(params); }
同样的GridView也一样的.
public class SuperGridView extends GridView { public SuperGridView(Context context, AttributeSet attrs) { super(context, attrs); } public SuperGridView(Context context) { super(context); } public SuperGridView(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(ScrollView)嵌套ListView、GridView显示不全,内容大于一行计算的高度不正确的解决办法
- scrollview嵌套listview,gridview 显示不全万能解决办法
- ListView嵌套GridView,显示不全解决办法
- android ScrollVIew中嵌套GridView,和ListView显示不全解决方法
- ListView嵌套GridView导致数据显示不完整的解决办法
- 用于解决listview嵌套GridView时显示不全的问题。
- Android 解决 ScrollView嵌套ListView、GridView冲突显示不全的最快实现
- ListView嵌套在ScrollView内显示不全的解决办法
- android ScrollView中嵌套GridView,ListView只显示一行的解决办法
- ScrollView中嵌套GridView,ListView只显示一行的解决办法
- ListView嵌套GridView显示不全解决方法
- 解决ScrollView下嵌套ListView、GridView显示不全的问题
- 解决ScrollView下嵌套ListView、GridView显示不全的问题
- android ScrollView中嵌套GridView,ListView只显示一行的解决办法
- ListView嵌套GridView显示不全解决方法
- ScrollView中嵌套GridView,ListView只显示一行的解决办法
- 解决ScrollView下嵌套ListView、GridView显示不全的问题(冲突)
- ListView中嵌套GridView显示不全的解决方法
- 解决ScrollView下嵌套ListView、GridView显示不全的问题(冲突)
- Android ScrollView中嵌套GridView,ListView只显示一行的解决办法