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

时间:2010-11-15 12:58:30

标签: android state expandablelistview

我有一个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);
}

3 个答案:

答案 0 :(得分:11)

不幸的是,只要焦点丢失,扩展状态总会重置,并且当再次获得焦点但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;
    }
}

可能有人有更好的解决方案。

答案 1 :(得分:2)

这应该有效

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

答案 2 :(得分:0)

试试这个。这将解决问题,部分

yourlist.setSaveEnabled(true);