How To Save And Restore The State Of An Expandablelistview In Android?
Solution 1:
I iterate through the groups and save the state of them all:
int numberOfGroups = MyExpandableListViewAdapter.getGroupCount();
boolean[] groupExpandedArray = newboolean[numberOfGroups];
for (int i=0;i<numberOfGroups;i++)
groupExpandedArray[i] = MyExpandableListView.isGroupExpanded(i);
Then for restore the state:
for (int i=0;i<groupExpandedArray.length;i++)
if (groupExpandedArray[i] == true)
MyExpandableListView.expandGroup(i);
I don't understand what you mean by how to access the ListView in onPause() / onResume(), it should be accessible from there if you store the ListView object as a member of the activity class.
Solution 2:
Improving Floaf's answer: declare
publicstaticint firstVisiblePosition=0;
on Pause
int numberOfGroups = MyExpandableListViewAdapter.getGroupCount();
boolean[] groupExpandedArray = newboolean[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 group's state but it will also scroll to the childView that was viewed when activity paused.
Solution 3:
a simpler way could be, if you're using setOnGroupClickListner and setOnChildClickListener just keep the last selected ChildPosition and GroupPositin integer and later on check it and respond properly,
if (childPosition > 0) {
mExpandableList.expandGroup(groupPositin);
}
mExpandableList.setItemChecked(groupPositin + childPosition , true);
you just need to remember set childPosition to 0 in your setOnGroupListener method.
Solution 4:
I came across this very same question and found a better answer. I have a fragment and use the onSaveInstanceState and onViewStateRestored to save and restore the ExpandableListView.
Here I save the state of the list
@OverridepublicvoidonSaveInstanceState( @NonNull Bundle outState )
{
super.onSaveInstanceState( outState );
ExpandableListViewexpandable= getView() != null ? getView().findViewById( R.id.expandable ) : null;
if( expandable != null )
{
intgroupsCount= expandable.getExpandableListAdapter()
.getGroupCount();
boolean[] groupExpandedArray = newboolean[groupsCount];
for( inti=0; i < groupsCount; i += 1 )
{
groupExpandedArray[i] = expandable.isGroupExpanded( i );
}
outState.putBooleanArray( "groupExpandedArray", groupExpandedArray );
outState.putInt( "firstVisiblePosition", expandable.getFirstVisiblePosition() );
}
}
And here I restore it when it is needed
@OverridepublicvoidonViewStateRestored( @Nullable Bundle savedInstanceState )
{
super.onViewStateRestored( savedInstanceState );
if( savedInstanceState != null )
{
boolean[] groupExpandedArray = savedInstanceState.getBooleanArray( "groupExpandedArray" );
intfirstVisiblePosition= savedInstanceState.getInt( "firstVisiblePosition", -1 );
ExpandableListViewexpandable= getView() instanceof ViewGroup ? getView().findViewById( R.id.expandable ) : null;
if( expandable != null && groupExpandedArray != null )
{
for( inti=0; i < groupExpandedArray.length; i++ )
{
if( groupExpandedArray[i] )
expandable.expandGroup( i );
}
if( firstVisiblePosition >= 0 )
expandable.setSelection( firstVisiblePosition );
}
}
}
Post a Comment for "How To Save And Restore The State Of An Expandablelistview In Android?"