After maintaining collapse/expand state for N-level or Multilevel expandablelistview some subgroups are not displayed

 ̄綄美尐妖づ 提交于 2019-12-05 01:00:33
BNK

I have the same issue before with N-level Expandable ListView, I use a trick that displays only one expand item. Although it does not look like the best solution for you, however, hope it helps!

            // display only one expand item
            mExpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
                int previousGroup = -1;
                @Override
                public void onGroupExpand(int groupPosition) {
                    if (groupPosition != previousGroup)
                        mExpandableListView.collapseGroup(previousGroup);
                    previousGroup = groupPosition;
                }
            });

and the same on secondLevelExpListView

UPDATE:

Another way you can try as the following (together with above setOnGroupExpandListener):

public class CustomExpListView extends ExpandableListView
{
    public CustomExpListView(Context context)
    {
        super(context);
    }
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(2000, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

Then inside your ParentLevelAdapter call this:

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        final CustomExpListView secondLevelExpListView = new CustomExpListView(this.mContext);
        String parentNode = (String) getGroup(groupPosition);
        secondLevelExpListView.setAdapter(new SecondLevelAdapter(this.mContext, mListData_SecondLevel_Map.get(parentNode), mListData_ThirdLevel_Map));
        secondLevelExpListView.setGroupIndicator(null);
        secondLevelExpListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            int previousGroup = -1;
            @Override
            public void onGroupExpand(int groupPosition) {
                if (groupPosition != previousGroup)
                    secondLevelExpListView.collapseGroup(previousGroup);
                previousGroup = groupPosition;
            }
        });
        return secondLevelExpListView;
    }

If you don't want to use setOnGroupExpandListener, then you should increase heightMeasureSpec like this heightMeasureSpec = MeasureSpec.makeMeasureSpec(20000, MeasureSpec.AT_MOST);

Please note that I have only tested with 3-level expandable list view, you can read more at my answer at the following question:

How to add Three Level ListView in ExpandableListView in android

I have posted my full project to GitHub

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!