项目中各种奇葩需求,记录一下 下次直接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);
}
本文分享了如何在项目中根据子项高度动态设置ExpandableListView的高度,包括测量每个视图高度并存储,最后调整ListView布局参数的方法,便于处理各种特殊需求。
973

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



