解决scrollview同时嵌套listview和gridview 显示高度问题【listview和gridview都只显示了一行】
针对了listview :写一个工具类
public class ListHeightUtils {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
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));
listView.setLayoutParams(params);
}
}
activity中调用:ListHeightUtils.setListViewHeightBasedOnChildren(lv);// 调整listview高度
针对gridview:重写gridview
public class SquareGridView extends GridView {
public SquareGridView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
本文提供了解决ScrollView中同时嵌套ListView和GridView显示高度问题的方法,包括为ListView创建工具类来设置高度,以及重写GridView以适应不同尺寸需求。详细步骤和代码示例帮助开发者轻松调整布局,确保所有视图正确显示。
501

被折叠的 条评论
为什么被折叠?



