When I expand a new group, can I collapse the last one expanded?
Asked
Active
Viewed 4.4k times
5 Answers
122
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;
}

Ragunath Jawahar
- 19,513
- 22
- 110
- 155

danh32
- 6,234
- 2
- 38
- 39
-
2No problem. Did this for an app a while back. Glad it could help someone else. – danh32 Nov 30 '10 at 16:48
-
4There is a problem with your solution: the new expanded group will be scrolled as far as the height of the last group (which is closed). But thanks anyway. – Anh Tuan Feb 27 '12 at 05:00
-
To avoid keeping a instance variabel called lastExpandedGroupPosition you can simply loop over your whole lists index, and then close all which isn't the new group that is getting expanded. – Johan S Mar 23 '13 at 07:51
-
1@JohanS isn't that overkill?To prevent keeping an int you loop through a potentially huge list – Juan Cortés Jun 07 '13 at 20:28
23
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;
}

Community
- 1
- 1

cockadoodledo
- 371
- 1
- 3
- 10
-
-
Even though this technically does the trick, it doesn't look pretty. Any recommendations to make it look better? – erkinyldz Jun 19 '15 at 13:10
-
Okay I'm answering my own question. If you call parent.setSelection(groupPosition) it looks much better. Keep in mind, it needs to be called after group expanding or collapsing logic, not before. – erkinyldz Jun 19 '15 at 13:17
17
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;
}
});
4
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.

Himanshu
- 31,810
- 31
- 111
- 133

Driss Bounouar
- 3,182
- 2
- 32
- 49
2
@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);
}

raja
- 2,393
- 2
- 22
- 25