androidexpandablelistview

Collapse all group except selected group in expandable listview android


I'm developing android application using expandable list view. Actually what I need is, I'm listing group, which contains child.

If I select an unexpandable group, it should expand, after I ll select second group at that time the first group should be collapsed. I did Google, but I couldn't find what I want. Please help me out.


Solution

  • Have the current expanded group position stored in a variable. In onGroupExpanded do the following.

    private int lastExpandedPosition = -1;
    private ExpandableListView lv; //your expandable listview
    ...
    
    lv.setOnGroupExpandListener(new OnGroupExpandListener() {
    
        @Override
        public void onGroupExpand(int groupPosition) {
                if (lastExpandedPosition != -1
                        && groupPosition != lastExpandedPosition) {
                    lv.collapseGroup(lastExpandedPosition);
                }
                lastExpandedPosition = groupPosition;
        }
    });