How to save and restore ExpandableListView state in Android?

Is it possible to save and restore the state (which elements are minimized and which are not) ExpandableListView in Android?

If possible, how can I do this?

Is it possible to access ExpandableListView in onPause () / onResume () and how?

+2
android state save expandablelistview restore
source share
3 answers

I iterate over the groups and save the state of all of them:

int numberOfGroups = MyExpandableListViewAdapter.getGroupCount(); boolean[] groupExpandedArray = new boolean[numberOfGroups]; for (int i=0;i<numberOfGroups;i++) groupExpandedArray[i] = MyExpandableListView.isGroupExpanded(i); 

Then to restore the state:

 for (int i=0;i<groupExpandedArray.length;i++) if (groupExpandedArray[i] == true) MyExpandableListView.expandGroup(i); 

I do not understand what you mean by accessing the ListView in onPause () / onResume (), it should be accessible from there if you save the ListView object as a member of the activity class.

+4
source share

Improving Floaf Response: declare

  public static int firstVisiblePosition=0; 

when paused

 int numberOfGroups = MyExpandableListViewAdapter.getGroupCount(); boolean[] groupExpandedArray = new boolean[numberOfGroups]; for (int i=0;i<numberOfGroups;i++){ groupExpandedArray[i] = MyExpandableListView.isGroupExpanded(i); } firstVisiblePosition = MyExpandableListView.getFirstVisiblePosition(); 

onResume

 for (int i=0;i<groupExpandedArray.length;i++){ if (groupExpandedArray[i] == true) MyExpandableListView.expandGroup(i); } MyExpandableListView.setSelection(firstVisiblePosition ); 

This will not only restore each state of the group, but also result in viewing the child that was viewed when the activity was paused.

+3
source share

it might be easier if you use setOnGroupClickListner and setOnChildClickListener, just save the last selected number of ChildPosition and GroupPositin, and then check it and answer correctly,

 if (childPosition > 0) { mExpandableList.expandGroup(groupPositin); } mExpandableList.setItemChecked(groupPositin + childPosition , true); 

you just need to remember that set childPosition is set to 0 in the setOnGroupListener method.

0
source share

All Articles