Collapse all group except selected group in expandable listview android

Sathish picture Sathish · Jul 11, 2013 · Viewed 49k times · Source

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.

Answer

user936414 picture user936414 · Jul 11, 2013

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;
    }
});