ScrollView里面嵌套ExpandableListView時(shí)响鹃,ExpandableListView顯示不全敛劝。
解決辦法:
對(duì)于ExpandableListView的展開收起計(jì)算出所有顯示的子項(xiàng)的高度适秩。
通過setOnGroupExpandListener 和 setOnGroupCollapseListener對(duì)一階子項(xiàng)的展開狀態(tài)監(jiān)聽
/**
* 設(shè)置組的子View的高度
* @param listView
* @param groupPosition
* @param isExpanded
*/
public static void setChildViewHeight(ExpandableListView listView, int groupPosition, Boolean isExpanded) {
ExpandableListAdapter listAdapter = listView.getExpandableListAdapter();
int childTotalHeight = 0;
for (int i = 0; i < listAdapter.getChildrenCount(groupPosition); i++) {
View child = listAdapter.getChildView(groupPosition, i, false, null, listView);
child.measure(0, 0);
childTotalHeight += child.getMeasuredHeight();
}
ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
if (isExpanded) {//展開狀態(tài)绊序,增加高度
layoutParams.height += childTotalHeight;
} else {//收起狀態(tài)硕舆,減掉二階子項(xiàng)高度
layoutParams.height -= childTotalHeight;
}
listView.setLayoutParams(layoutParams);
}
/**
* 設(shè)置組的高度
* @param listView
*/
public static void setGroupViewHeight(ExpandableListView listView) {
int groupTotalHeight = 0;
ExpandableListAdapter listAdapter = listView.getExpandableListAdapter();
for (int i = 0; i < listAdapter.getGroupCount(); i++) {
View groupItem = listAdapter.getGroupView(i, false, null, listView);
groupItem.measure(0, 0);
groupTotalHeight += groupItem.getMeasuredHeight();
}
ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
layoutParams.height = groupTotalHeight + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));;
listView.setLayoutParams(layoutParams);
}