2

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?

Sourav
  • 1,214
  • 14
  • 24
  • a bit of irrelevant, ExpandableListView.expandGroup() takes an argument of int, but why is selectedGroupPosition a String? – kevin May 28 '14 at 09:52
  • Try this SO answer [http://stackoverflow.com/a/4315162/2083078]. It is very simple and elegant solution. – Manish Mulimani May 28 '14 at 10:12

2 Answers2

3

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;
  }
} );
injecteer
  • 20,038
  • 4
  • 45
  • 89
0

Try this (added two parent.expandGroup() statements)

expListView.setOnGroupClickListener(new OnGroupClickListener() {
     @Override
     public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
        if (groupPosition==0) {
           if(parent.isGroupExpanded(1))
              parent.collapseGroup(1);

           parent.expandGroup(0);
        } 
        else if (groupPosition==1) {
            if(parent.isGroupExpanded(0))
              parent.collapseGroup(0);

            parent.expandGroup(1);
        }

        parent.smoothScrollToPosition(groupPosition);

         selectedGroupPosition = ""+groupPosition;
         return false;
     }
 });
kevin
  • 807
  • 3
  • 11
  • 20