Programmatically collapse a group in ExpandableListView

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

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

相关标签:
5条回答
  • 2020-11-28 22:19

    Do this to expand the clicked group and collapse all others

    public void onGroupExpand(int groupPosition)
    {
        for (int i = 0; i < len; i++)
        {
            if (i != groupPosition)
            {
                expandableListDetailsLevel.collapseGroup(i);
            }
        }
    }
    

    It's working for me.

    0 讨论(0)
  • 2020-11-28 22:22
        @Override
        public void onGroupExpanded(int groupPosition) {
            for(int i=0;i<mGroupCollection.size();i++){
                if(i==groupPosition){
                    System.out.println("Nothing");
                             }
                else{
                       mExpandableListView.collapseGroup(i);
                }
    
            }
            super.onGroupExpanded(groupPosition);
        }
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-11-28 22:31

    This worked for me

    expandableList.setOnGroupExpandListener(new OnGroupExpandListener() {
        int previousItem = -1;
    
        @Override
        public void onGroupExpand(int groupPosition) {
            if(groupPosition != previousItem )
                expandableList.collapseGroup(previousItem );
            previousItem = groupPosition;
        }
    });
    
    0 讨论(0)
  • 2020-11-28 22:39

    Try putting this in your ExpandableListAdapter, listView is a reference to the ExpandableListView itself. And lastExpandedGroupPosition is a integer member variable defined inside your ExpandableListAdapter.

        @Override
        public void onGroupExpanded(int groupPosition){
            //collapse the old expanded group, if not the same
            //as new group to expand
            if(groupPosition != lastExpandedGroupPosition){
                listView.collapseGroup(lastExpandedGroupPosition);
            }
    
            super.onGroupExpanded(groupPosition);           
            lastExpandedGroupPosition = groupPosition;
        }
    
    0 讨论(0)
提交回复
热议问题