Collapse all group except selected group in expandable listview android
Solution 1:
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;
}
});
Solution 2:
Use this code this will work
expandableList.setOnGroupExpandListener(new OnGroupExpandListener() {
int previousItem = -1;
@Override
public void onGroupExpand(int groupPosition) {
if(groupPosition != previousItem )
expandableList.collapseGroup(previousItem );
previousItem = groupPosition;
}
});
Solution 3:
@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;
}
Solution 4:
If the ExpandableListView has more than 2 groups:
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
for (int g = 0; g < expandableListAdapter.getGroupCount(); g++) {
if (g != groupPosition) {
expandableListView.collapseGroup(g);
}
}
}
});
It will collapse all groups except the clicked one.