Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preserve scroll position in an ExpandableListView

Tags:

android

in my app I have a class derived from ExpandableListActivity. When I scroll the contents, and then change phone orientation or edit an item and then go back to the list, the original list position is not preserved. I have tried two solutions, similar to those I have recently successfully used with ListActivity-derived classes.

Solution 1:

protected void onRestoreInstanceState(Bundle state) {
    super.onRestoreInstanceState(state);
    mListState = state.getParcelable(LIST_STATE);
}

protected void onResume() {
    super.onResume();
    loadData();
    if (mListState != null)
        getExpandableListView().onRestoreInstanceState(mListState);
}

protected void onSaveInstanceState(Bundle state) {
    super.onSaveInstanceState(state);
    mListState = getExpandableListView().onSaveInstanceState();
    state.putParcelable(LIST_STATE, mListState);
}

Solution 2:

protected void onRestoreInstanceState(Bundle state) {
    super.onRestoreInstanceState(state);
    mListPosition = state.getInt(LIST_STATE);
}

protected void onResume() {
    super.onResume();
    loadData();
    getExpandableListView().setSelection(mListPosition);
}

protected void onSaveInstanceState(Bundle state) {
    super.onSaveInstanceState(state);
    mListPosition = getExpandableListView().getFirstVisiblePosition();
    state.putInt(LIST_STATE, mListPosition);
}

Neither solution works. I have also tried to combine the two solutions, and this works when I edit an item and go back to the list, but it does NOT work when I change the phone orientation

Anyone can suggest a good way to achieve my goal?

like image 473
Giorgio Barchiesi Avatar asked Apr 19 '11 08:04

Giorgio Barchiesi


People also ask

How can I retain the scroll position of a scrollable area when pressing back button?

store the position in cookies and after that use the cookie to scroll the page to exact position .

How to Save state of ListView in android?

// Save the ListView state (= includes scroll position) as a Parceble Parcelable state = listView. onSaveInstanceState(); // e.g. set new items listView. setAdapter(adapter); // Restore previous state (including selected item index and scroll position) listView.

How do you make an expanded list in flutter?

Expandable ListView is a type of list view that is used to show multiple types of data based on category and subcategory. Expandable list view is used to expand or collapse the view in list items In a flutter app. We can easily make our own Expandable ListView using the ExpansionTile widget.


2 Answers

After a few experiments I have come to a satisfactory solution, which also preserves the fine scroll position of the top visible item.

As a matter of fact, three different pieces of information need to be saved and restored: the list state (e.g. which groups are expanded), the index of the first visible item, and its fine scroll position.

Unfortunately, it seems that only the first one is saved by the onSaveInstanceState method of the expandable list view, so the other two need to be saved separately. This is different from a non expandable list view, where it appears the onSaveInstanceState method saves all the information needed to properly restore state and position of the list (on this subject, see Maintain/Save/Restore scroll position when returning to a ListView).

Here are the code snippets; on top of the ExpandableListActivity-derived class:

private static final String LIST_STATE_KEY = "listState";
private static final String LIST_POSITION_KEY = "listPosition";
private static final String ITEM_POSITION_KEY = "itemPosition";

private Parcelable mListState = null;
private int mListPosition = 0;
private int mItemPosition = 0;

Then, some overrides:

protected void onRestoreInstanceState(Bundle state) {
    super.onRestoreInstanceState(state);

    // Retrieve list state and list/item positions
    mListState = state.getParcelable(LIST_STATE_KEY);
    mListPosition = state.getInt(LIST_POSITION_KEY);
    mItemPosition = state.getInt(ITEM_POSITION_KEY);
}

protected void onResume() {
    super.onResume();

    // Load data from DB and put it onto the list
    loadData();

    // Restore list state and list/item positions
    ExpandableListView listView = getExpandableListView();
    if (mListState != null)
        listView.onRestoreInstanceState(mListState);
    listView.setSelectionFromTop(mListPosition, mItemPosition);
}

protected void onSaveInstanceState(Bundle state) {
    super.onSaveInstanceState(state);

    // Save list state
    ExpandableListView listView = getExpandableListView();
    mListState = listView.onSaveInstanceState();
    state.putParcelable(LIST_STATE_KEY, mListState);

    // Save position of first visible item
    mListPosition = listView.getFirstVisiblePosition();
    state.putInt(LIST_POSITION_KEY, mListPosition);

    // Save scroll position of item
    View itemView = listView.getChildAt(0);
    mItemPosition = itemView == null ? 0 : itemView.getTop();
    state.putInt(ITEM_POSITION_KEY, mItemPosition);
}

This works fine on my Froyo device.

like image 58
Giorgio Barchiesi Avatar answered Sep 17 '22 18:09

Giorgio Barchiesi


This is how I got ExpandableListView.onRestoreInstanceState and onSaveInstanceState to work...

If you're using BaseExpandableListAdapter the default implementation of getCombinedChildId returns a negative number. If you look into the code for restoring a list from saved state scroll position it ignores ids which are negative. Return a positive IDs so that the AbsListView onRestoreInstanceState will set the scroll position properly. To return positive IDs change the or of getCombinedChildId from 0x8000000000000000L (negative) to 0x7000000000000000L (positive) by over-riding the method in your subclass of BaseExpandableListAdapter as follows...

        public long getCombinedChildId(long groupId, long childId)
        {
            long or  = 0x7000000000000000L;
            long group = (groupId & 0x7FFFFFFF) << 32;
            long child = childId & 0xFFFFFFFF;
            return or | group | child;
        }
like image 34
Cal Avatar answered Sep 20 '22 18:09

Cal