11

ユーザーが子要素をクリックすると別のアクティビティを開始する 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);
}
4

3 に答える 3

11

残念ながら、フォーカスが失われると常に展開状態がリセットされ、再びフォーカスを取得したときに onCreate() は呼び出されず、onStart() が呼び出されます。したがって、現時点での私の回避策は、展開されたすべてのアイテムの 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;
    }
}

誰かがより良い解決策を持っているかもしれません。

于 2010-11-15T16:15:24.237 に答える
2

これはうまくいくはずです

Parcelable state = exercisesList.onSaveInstanceState();
exercisesList.setAdapter(....);
exercisesList.onRestoreInstanceState(state);
于 2013-04-21T19:26:22.253 に答える
0

これを試して。それは部分的に問題を解決します

yourlist.setSaveEnabled(true);
于 2010-11-15T13:16:05.740 に答える