解决方案一:
/**
* 动态设置ListView高度,解决ScrollView 与 ListView, 部分手机反应有空指针问题,所以推荐使用方案二
* @param listView
*/
private void setListViewHeightBasedOnChildren(ListView listView) {
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));
listView.setLayoutParams(params);
}
解决方案二:
自定义ListView, 复写其onMeasure方法,如下
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
ListView被ScrollView包裹时,高度为0, 如何解决?
最新推荐文章于 2024-08-08 09:23:31 发布