问题 保存并恢复ExpandableListActivity的展开/折叠状态


我有一个ExpandableListActivity(使用SimpleCursorTreeAdapter),当用户点击子元素时,它会启动另一个活动。在新活动中按后退按钮时,所有列表项将再次折叠。如何保存ExpandableListActivity的展开状态并再次还原它。

我已经尝试像这样实现onSaveInstanceState()和onRestoreInstanceState()......

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Parcelable listState = getExpandableListView().onSaveInstanceState();
    outState.putParcelable("ListState", listState);
}


@Override
protected void onRestoreInstanceState(Bundle state) {
    super.onRestoreInstanceState(state);
    Parcelable listState = state.getParcelable("ListState");
    getExpandableListView().onRestoreInstanceState(listState);
}

...但是onRestoreInstanceState()从未被调用过。我还尝试在onCreate()方法中恢复状态,但它也没有被调用:

if (savedInstanceState != null) {
    Parcelable listState = savedInstanceState.getParcelable("ListState");
    getExpandableListView().onRestoreInstanceState(listState);
}

7905
2017-11-15 12:58


起源

我正在做类似的事情 stackoverflow.com/questions/10611927/... - toobsco42
@ toobsco42我不知道这与这个问题有什么关系 - Tom
返回上一个活动时,请注意再次设置适配器。重新设置适配器将折叠列表。 - jayeffkay


答案:


不幸的是,只要焦点丢失,扩展状态总是重置,并且当再次获得焦点但onStart()时不调用onCreate()。所以我现在的解决方法是手动存储所有扩展项的ID,并再次在onStart()中展开它们。我实现了ExpandableListActivity的子类来重用该行为。

public class PersistentExpandableListActivity extends ExpandableListActivity {
    private long[] expandedIds;

    @Override
    protected void onStart() {
        super.onStart();
        if (this.expandedIds != null) {
            restoreExpandedState(expandedIds);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        expandedIds = getExpandedIds();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        this.expandedIds = getExpandedIds();
        outState.putLongArray("ExpandedIds", this.expandedIds);
    }

    @Override
    protected void onRestoreInstanceState(Bundle state) {
        super.onRestoreInstanceState(state);
        long[] expandedIds = state.getLongArray("ExpandedIds");
        if (expandedIds != null) {
            restoreExpandedState(expandedIds);
        }
    }

    private long[] getExpandedIds() {
        ExpandableListView list = getExpandableListView();
        ExpandableListAdapter adapter = getExpandableListAdapter();
        if (adapter != null) {
            int length = adapter.getGroupCount();
            ArrayList<Long> expandedIds = new ArrayList<Long>();
            for(int i=0; i < length; i++) {
                if(list.isGroupExpanded(i)) {
                    expandedIds.add(adapter.getGroupId(i));
                }
            }
            return toLongArray(expandedIds);
        } else {
            return null;
        }
    }

    private void restoreExpandedState(long[] expandedIds) {
        this.expandedIds = expandedIds;
        if (expandedIds != null) {
            ExpandableListView list = getExpandableListView();
            ExpandableListAdapter adapter = getExpandableListAdapter();
            if (adapter != null) {
                for (int i=0; i<adapter.getGroupCount(); i++) {
                    long id = adapter.getGroupId(i);
                    if (inArray(expandedIds, id)) list.expandGroup(i);
                }
            }
        }
    }

    private static boolean inArray(long[] array, long element) {
        for (long l : array) {
            if (l == element) {
                return true;
            }
        }
        return false;
    }

    private static long[] toLongArray(List<Long> list)  {
        long[] ret = new long[list.size()];
        int i = 0;
        for (Long e : list)  
            ret[i++] = e.longValue();
        return ret;
    }
}

也许某人有更好的解决方案。


11
2017-11-15 16:15





这应该工作

Parcelable state = exercisesList.onSaveInstanceState();
exercisesList.setAdapter(....);
exercisesList.onRestoreInstanceState(state);

2
2018-04-21 19:26



接受的答案完全没必要。这是正确的方法。 - JanithaR


尝试这个。它将部分地解决问题

yourlist.setSaveEnabled(true);

0
2017-11-15 13:16



不幸的是,这并没有改变什么。 - Tom
它仅适用于屏幕的更改方向(如果在更改屏幕方向后不会重新加载适配器的数据) - user479211