在scrollview中嵌套Expandlistview,如果不做处理,expandlistview活显示不全,需要重新进行测量,获取高度,动态设置Expandlistview高度。
代码
public static void setExpandListViewHeight(ExpandableListView listView) {
ExpandableListAdapter listAdapter = listView.getExpandableListAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
//获取所有expandlsitview的父条目
for (int i = 0; i < listAdapter.getGroupCount(); i++) {
View group = listAdapter.getGroupView(i, true, null, listView);
//重新测量,重新申请合适的高和宽进行放置view
group.measure(0, 0);
totalHeight += group.getMeasuredHeight() + listView.getDividerHeight();
//索取展开的子条目高度
if (listView.isGroupExpanded(i)) {
for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
View child = listAdapter.getChildView(i, j, false, null, listView);
child.measure(0, 0);
totalHeight += child.getMeasuredHeight() + listView.getDividerHeight();
}
}
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
if (totalHeight > DensityUtil.getScreenHeight() - PhoneDensityUtils.dip2px(56 + 6) -
PhoneDensityUtils.getStatusHeight()) {
totalHeight += PhoneDensityUtils.dip2px(56 + 6);
} else {
totalHeight += PhoneDensityUtils.dip2px(12);
}
params.height = totalHeight;
listView.setLayoutParams(params);
}
获取expandlistview高度
最新推荐文章于 2021-05-29 11:06:52 发布