项目中各种奇葩需求,记录一下 下次直接copy
public void setExpandableListViewHeightBasedOnChildren(ExpandableListView listView) {
ExpandableListAdapter listAdapter = listView.getExpandableListAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
height.clear();
for (int i = 0; i < listAdapter.getGroupCount(); i++) {
View listItem = listAdapter.getGroupView(i, true, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
View childView = listAdapter.getChildView(i, j, false, null,
listView);
childView.measure(0, 0);
totalHeight += childView.getMeasuredHeight();
}
// 存储组的位置,不需要可删除
height.add(totalHeight);
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
listView.setLayoutParams(params);
}