gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:02:09 26 4
gpt4 key购买 nike

我有一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com