单击空组时,可展开列表视图会折叠其他组

时间:2018-11-29 03:46:50

标签: android expandablelistview

我遇到了问题。我习惯于此代码折叠可扩展列表视图android中除选定组以外的所有组:

mExpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                if (lastExpandedPosition != -1 && groupPosition != lastExpandedPosition) {
                    mExpandableListView.collapseGroup(lastExpandedPosition);
                }
                lastExpandedPosition = groupPosition;
            }
        });

但是某些组是空的,因此我希望单击空组不会关闭其他组。 谢谢

1 个答案:

答案 0 :(得分:0)

尝试一下:

    mExpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            ExpandableListViewAdapter adapter = (ExpandableListViewAdapter)mExpandableListView.getAdapter();
            if (adapter.getChildrenCount(groupPosition) > 0) {
                if (lastExpandedPosition != -1 && groupPosition != lastExpandedPosition) {
                    mExpandableListView.collapseGroup(lastExpandedPosition);
                }
                lastExpandedPosition = groupPosition;
            }
        }
    });

请更改适配器类型以匹配您的适配器类型。希望有帮助!