【问题标题】:Save and restore expanded/collapsed state of an ExpandableListActivity保存和恢复 ExpandableListActivity 的展开/折叠状态
【发布时间】:2011-05-10 05:34:03
【问题描述】:

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

【问题讨论】:

  • 我在这里做类似的事情stackoverflow.com/questions/10611927/…
  • @toobsco42 我看不出这与问题有什么关系
  • 返回上一个活动时要注意再次设置适配器。重新设置适配器将折叠列表。

标签: android state expandablelistview


【解决方案1】:

试试这个。它会部分解决问题

yourlist.setSaveEnabled(true);

【讨论】:

  • 不幸的是,这并没有改变任何东西。
  • 它只适用于改变屏幕方向(如果改变屏幕方向后适配器的数据不会重新加载)
【解决方案2】:

不幸的是,每当失去焦点时展开状态总是会重置,并且当它再次获得焦点时不会调用 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;
    }
}

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

【讨论】:

    【解决方案3】:

    这应该可以工作

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

    【讨论】:

    • 接受的答案是完全没有必要的。这是正确的方法。
    猜你喜欢
    • 2012-08-26
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多