18

I use

SimpleExpandableListAdapter

in my

ExpandableListActivity

When user click the group row, the children list is expanded and show, when user click each child item in the expanded list, user will be navigated to the next second_Activity. Currently, when user click the back button to go back from second_Activity to the ExpandableListActivity, the expandable list is initialized as un-expanded, I would like the expandable list keep in expanded status, so that user know previously which item he has selected. How to do that? Which method from ExpandableListActivity should be override?

PS: I know I should put getExpandableListView().expandGroup(groupPosition); somewhere, but where? I tried to put this code in onGroupClick() and onChildClick(), but this does not help.

Mellon
  • 37,586
  • 78
  • 186
  • 264

6 Answers6

65

For always expanded groups, I extend SimpleExpandableListAdapter and set the group to be expanded in the getGroupView method:

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    View v = super.getGroupView(groupPosition, isExpanded, convertView, parent);
    ExpandableListView eLV = (ExpandableListView) parent;
    eLV.expandGroup(groupPosition);
    return v;
}
straya
  • 5,002
  • 1
  • 28
  • 35
  • There seems to be a few problems when doing it this way. Apparently (though I haven't checked) the [ListView loses focus after scrolling](http://howrobotswork.wordpress.com/2013/05/26/keep-groups-expanded-in-expandablelistview). In my case though, it was occasionally causing a random NullPointerException during ExpandableListView.getFlatListPosition(), when getting the first child of a group. Manually expanding each group after setting the adapter seemed to solve it. – technicalflaw Oct 31 '13 at 18:03
  • WARNING: Not sufficient, if you dynamically add views in an empty group: you need to add the `parent.expandGroup` in **getChildView** either – cyrilchampier Apr 14 '14 at 08:15
  • Haven't experienced the issues, but this is far more simple when you add whole new groups compared expanding everything every time you change something. – Warpzit Jan 23 '15 at 12:19
9

I was browsing around trying to find a solution to my own ExpandableListView problem. Anyway, add my 2 cents to the answer provided by adstro.

The concept is simple: First call the getGroupCount() function of the ExpandableList Adapter to find the number of groups Then, loop through the groups and call the expandGroup(int GroupPos) function of the ExpandableList View to expand each group.

The above codes should be put inside onCreate() and also the onResume() to cater for both the first creation and the return to the activity subsequently after creation.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Yam
  • 143
  • 2
  • 7
  • 7
    Putting it only onResume should be enough, since anything that goes through onCreate will also go through onResume. – xaethos Aug 22 '12 at 01:56
3

I did something similar, except I keep all the groups expanded all the time. To accomplish this, I got a handle to the listView via ExpandableListActivity.getExpandableListView() and use ExpandableListView.expandGroup (int groupPos) to expand the groups.

For your scenario, you could keep track of which group(s) is/are expanded and once the activity to loaded again, re-expand them.

Hope this helps.

One other thing...I put this code in OnCreate().

adstro
  • 688
  • 1
  • 9
  • 17
  • adstro,could you post the code,i am unable to understand it properly – ProgramME Jun 15 '11 at 14:18
  • 2
    @ProgramME he meant tmpListView.expandGroup(0); considering you wish to expand the group at position 0 if you wish to expand all the items do it in a for loop. – Jayshil Dave Oct 27 '11 at 11:02
2
 @Override
 View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup     parent) {
     View v = super.getGroupView(groupPosition, isExpanded, convertView, parent);
     ExpandableListView eLV = (ExpandableListView) parent;
     eLV.expandGroup(groupPosition);
     return v;
}

after that

     expListView.setOnChildClickListener(new OnChildClickListener() {

     @Override
     public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
         Toast.makeText(getApplicationContext(),listDataHeader.get(groupPosition)
             + " : " 
             + listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition),Toast.LENGTH_SHORT).show();
         return false;
     }
    });

in onResume...

Worked for me. No problem with scrolling.

2Dee
  • 8,609
  • 7
  • 42
  • 53
D-D
  • 954
  • 2
  • 12
  • 27
1

Solution provided by straya is not good for Android 2.3.3. Your ExpandableListView will lost focus and click events will stop working after list is scrolled.

Damian Petla
  • 8,963
  • 5
  • 44
  • 47
0

I have done this way:

ExpandableAdpater adapter=new ExpandableAdapter(context);
expandableListView.setAdapter(adapter);

for (int i = 0; i < listResponse.size(); i++) {
                                expandableListView.expandGroup(i);
                            }
Ravi Yadav
  • 2,296
  • 3
  • 25
  • 32