Android - NestedScrollView which contains ExpandableListView doesn't scroll when expanded

前端 未结 5 588
情书的邮戳
情书的邮戳 2020-11-29 08:40

I have an ExpandableListView inside a NestedScrollView (yes I know, it is not good to have a scrolling view inside another scrolling view but I don

5条回答
  •  一个人的身影
    2020-11-29 09:11

    Use this method this will calculate the ExpendableListSize at run time.

     private void setListViewHeight(ExpandableListView listView,
                                   int group) {
        ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
        int totalHeight = 0;
        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
                View.MeasureSpec.EXACTLY);
        for (int i = 0; i < listAdapter.getGroupCount(); i++) {
            View groupItem = listAdapter.getGroupView(i, false, null, listView);
            groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
    
            totalHeight += groupItem.getMeasuredHeight();
    
            if (((listView.isGroupExpanded(i)) && (i != group))
                    || ((!listView.isGroupExpanded(i)) && (i == group))) {
                for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
                    View listItem = listAdapter.getChildView(i, j, false, null,
                            listView);
                    listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
    
                    totalHeight += listItem.getMeasuredHeight();
    
                }
            }
        }
    
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        int height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
        if (height < 10)
            height = 200;
        params.height = height;
        listView.setLayoutParams(params);
        listView.requestLayout();
    
    }
    

    An call this method in your setOnGroupClickListener.like below

    mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
    
                setListViewHeight(parent, groupPosition);
                return false;
            }
        });
    

提交回复
热议问题