ScrollView与ExpandableListView冲突问题

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);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,581评论 25 708
  • 一个孩子从小有个摇滚梦,虽然长大了没成为歌手,却通过电影圆了自己的梦。 它最重要的作用大概是让我们还看到了那些让我...
    看电影的张三疯阅读 229评论 0 0
  • 小时候,我有最爱的两条手巾,一条是纯绿色的,另一条是粉色的,正中间坐着一只小白兔。我常常把他们用来当玩偶的被子,当...
    俗人半生阅读 356评论 0 0
  • 中国的四大名著虽然被影视改变许多版本,但还数西游记最容易为后人戏说。什么《大话西游》、《西游后传》、《沙僧的逆袭》...
    曼天星阅读 133评论 0 0