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

前端之家收集整理的这篇文章主要介绍了android – 保存并恢复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<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/311150.html

猜你在找的Android相关文章