在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 发布
当ExpandListView被嵌套在ScrollView中时,可能显示不全。为解决此问题,需要通过代码动态测量并设置ExpandListView的高度。文章提供了一段Java代码示例,通过遍历并测量所有父条目及展开的子条目来计算总高度,从而确保列表完整显示。

314

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



