I'm building an expandable listview, in which I want only one group to be expanded at a time.
For the time being I've only 2 groups in my listview. When the user clicks on a group, the other one should collapse and vice versa. Also, the same should be retained during orientation changes.
Activity Class:
private String selectedGroupPosition = null;
Orientation Change:
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
initUI();
}
initUI():
if(selectedGroupPosition!=null) {
expListView.expandGroup(Integer.parseInt(selectedGroupPosition));
}
........
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
parent.smoothScrollToPosition(groupPosition);
if (groupPosition==0) {
if(parent.isGroupExpanded(1))
parent.collapseGroup(1);
}
else if (groupPosition==1) {
if(parent.isGroupExpanded(0))
parent.collapseGroup(0);
}
selectedGroupPosition = ""+groupPosition;
return false;
}
});
However, this code does not seem to work. When I click on the 1st group, it expands. Next when I click on the 2nd group, the 1st group collapses but the 2nd one does NOT expand. (it only expands on a subsequent click)
But if I do an orientation change at this point of time the 2nd group expands.
Why is this so?
There's no such thing out-of-box, but you can build it yourself pretty easily. You need to add the listener to collapse the previously openned group:
expListView.setOnGroupExpandListener( new OnGroupExpandListener() {
int previousGroup = -1;
@Override public void onGroupExpand( int groupPosition ) {
if( groupPosition != previousGroup ) expListView.collapseGroup( previousGroup );
previousGroup = groupPosition;
}
} );