美文网首页Android开发
ScrollView与ExpandableListView冲突问

ScrollView与ExpandableListView冲突问

作者: Tonki | 来源:发表于2016-05-19 12:05 被阅读0次

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

    相关文章

      网友评论

        本文标题:ScrollView与ExpandableListView冲突问

        本文链接:https://www.haomeiwen.com/subject/lkjbrttx.html