[Java] 纯文本查看 复制代码
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter 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);
}
[Java] 纯文本查看 复制代码
public static void setListViewHeightBasedOnChildren(ExpandableListView listView ) {
// 获取ListView对应的Adapter
ExpandableListAdapter listAdapter = listView.getExpandableListAdapter();
if (listAdapter == null) {
// pre -condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getGroupCount(); i++) { // listAdapter.getCount()返回数据项的数目
View listgroupItem = listAdapter .getGroupView(i, true, null, listView );
listgroupItem.measure(0, 0); // 计算子项View 的宽高
totalHeight += listgroupItem .getMeasuredHeight(); // 统计所有子项的总高度
System. out.println("height : group" +i +"次" +totalHeight );
for (int j = 0; j < listAdapter.getChildrenCount( i); j++) {
View listchildItem = listAdapter .getChildView(i, j, false , null, listView);
listchildItem.measure(0, 0); // 计算子项View 的宽高
totalHeight += listchildItem.getMeasuredHeight(); // 统计所有子项的总高度
System. out.println("height :" +"group:" +i +" child:"+j+"次"+ totalHeight);
}
}
ViewGroup.LayoutParams params = listView .getLayoutParams();
params.height = totalHeight + ( listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
listView.setLayoutParams(params );
}