Programmatically collapse a group in ExpandableListView

后端 未结 5 609
無奈伤痛
無奈伤痛 2020-11-28 21:55

When I expand a new group, can I collapse the last one expanded?

5条回答
  •  星月不相逢
    2020-11-28 22:26

    Very helpful, but as Anh Tuan mentions in the comments above, I was having problems with the ExpandableListView not then scrolling back to the top of the currently selected group (it would stay at the currently scrolled position, in the middle of the group somewhere). You also need to add an onGroupClickListener() to scroll to the correct position:

    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition,
            long id) {
        // Implement this method to scroll to the correct position as this doesn't
        // happen automatically if we override onGroupExpand() as above
        parent.smoothScrollToPosition(groupPosition);
    
        // Need default behaviour here otherwise group does not get expanded/collapsed
        // on click
        if (parent.isGroupExpanded(groupPosition)) {
            parent.collapseGroup(groupPosition);
        } else {
            parent.expandGroup(groupPosition);
        }
    
        return true;
    }
    

提交回复
热议问题