lv_crowddetails.setAdapter(adapter);
setListViewHeightBasedOnChildren(lv_crowddetails);
需要重新测量listview的高度
protected void setListViewHeightBasedOnChildren(ListView lv_crowddetails2) {
// 获取ListView对应的Adapter
ListAdapter adapter = lv_crowddetails.getAdapter();
if (adapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0, len = adapter.getCount(); i < len; i++) {
// listAdapter.getCount()返回数据项的数目
View listItem = adapter.getView(i, null, lv_crowddetails);
// 计算子项View 的宽高
listItem.measure(0, 0);
// 统计所有子项的总高度
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = lv_crowddetails.getLayoutParams();
params.height = totalHeight+ (lv_crowddetails.getDividerHeight() * (adapter.getCount() - 1));
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
lv_crowddetails.setLayoutParams(params);
}