Android开发

ScrollView与ExpandableListView冲突问

2016-05-19  本文已影响0人  Tonki

ScrollView里面嵌套ExpandableListView时,ExpandableListView显示不全。

解决办法:

对于ExpandableListView的展开收起计算出所有显示的子项的高度。
通过setOnGroupExpandListenersetOnGroupCollapseListener对一阶子项的展开状态监听

/**
 * 设置组的子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) {//展开状态,增加高度
        layoutParams.height += childTotalHeight;
    } else {//收起状态,减掉二阶子项高度
        layoutParams.height -= childTotalHeight;
    }
    listView.setLayoutParams(layoutParams);
}

/**
 * 设置组的高度
 * @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);
}
上一篇下一篇

猜你喜欢

热点阅读