折叠组在多级可扩展列表视图中不起作用

时间:2017-11-08 00:03:11

标签: android expandablelistview

我有一个多级ExpandableListView。我需要一次只扩展一个项目。在第二级,我使用了这种方法,但它没有工作:

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

为什么方法collapseGroup(lastExpandedPositions)不响应? 任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

我的问题已经解决,答案在下面的代码中我将此代码添加到第一级可扩展列表适配器中的getchildView()方法中:

int  lastExpandedPositions=-1;

simpleExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
                @Override
                public boolean onGroupClick(ExpandableListView parent, View view,
                                            int groupPosition, long id) {

                    setListViewHeight(parent, groupPosition);

                       parent.smoothScrollToPosition(groupPosition);

                    // Need default behaviour here otherwise group does not get expanded/collapsed
                    if (parent.isGroupExpanded(groupPosition)) {
                        parent.collapseGroup(groupPosition);

                    } else {
                        parent.expandGroup(groupPosition);
                        if (lastExpandedPositions != -1
                                && groupPosition != lastExpandedPositions) {
                          parent.collapseGroup(lastExpandedPositions);



                        }
                        lastExpandedPositions = groupPosition;
                    }

                    return true;
                }
            });