获取expandlistview高度

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值