android - 保存和恢复 ExpandableListActivity 的展开/折叠状态

标签 android state expandablelistview

我有一个 ExpandableListActivity(使用 SimpleCursorTreeAdapter),它会在用户单击子元素时启动另一个 Activity 。在新 Activity 中按下后退按钮时,所有列表项都会再次折叠。如何保存 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);
}

最佳答案

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

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

关于android - 保存和恢复 ExpandableListActivity 的展开/折叠状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4184556/

相关文章:

android - 文件已加密或不是数据库(异常 net.sqlcipher.database.SQLiteException)

android - 在上下文操作栏可见时停止 ExpandableListView 组扩展

android - 禁用 ExpandableListView 组标题在滚动时移动

android - 可扩展的 Listview 父项和子项单独包装在一个布局中。

android - 简单的 Android 加减法应用程序

android - 如何在 Android Opencv C++ 中获取 abs 垫?

html - React onChange 处理程序在页面加载期间被多次调用

session - 在两个不同的应用程序之间传递 session 数据

java - 如何在android中从json数据在ImageView上显示图像

reactjs - React JS - 如何在对象内添加对象