您的位置:首页 > 其它

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  gridview listview 嵌套