一千萬個為什麽

搜索

保存並恢復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);
}

最佳答案

不幸的是,只要焦點丟失,擴展狀態總是重置,並且當再次獲得焦點但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 expandedIds = new ArrayList();
            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 list)  {
        long[] ret = new long[list.size()];
        int i = 0;
        for (Long e : list)  
            ret[i++] = e.longValue();
        return ret;
    }
}

也許某人有更好的解決方案。

轉載註明原文: 保存並恢復ExpandableListActivity的展開/折疊狀態