【问题标题】:ExpandableListView collapses all group items every time I come back from another Activity每次我从另一个 Activity 回来时,ExpandableListView 都会折叠所有组项
【发布时间】:2012-04-04 15:49:42
【问题描述】:
我需要有人指导我找出错误。当我接到一个电话或去另一个活动时,当我回来时,所有的组都被折叠了。系统没有销毁activity,所以我无法在onRestoreInstanceState中保存和恢复状态,因为该方法从未被调用(Activity只是暂停和停止,但没有被销毁)。
Activity 扩展 ExpandableListActivity,我使用的适配器是 SimpleCursorTreeAdapter。
在哪些情况下可展开列表折叠?我快疯了,我找不到解决办法。
提前致谢。
编辑:这是我的 Activity 的生命周期
当 Activivy 首次启动时:
onCreate() -> onStart() -> onResume()
何时我去其他活动:
onPause() -> onStop()
当我回来时:
onRestart -> onStart() -> onResume()
【问题讨论】:
标签:
android
expandablelistview
【解决方案1】:
不幸的是,每当失去焦点时展开状态总是会重置,并且当它再次获得焦点时不会调用 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;
}
}
也许有人有更好的解决方案。
【解决方案2】:
您可以通过在 Activity 中重写 onPause() 和 onResume() 来保存任何状态信息。
您应该能够保留所选项目,然后在 Activity 恢复时重新选择该项目。您也可以在 onStop() 和 onStart() 中添加它。
如果 Activity 只是被暂停/停止,那么所有的 Views 都应该保持它们的状态,因为它们不会被销毁和重新创建。尝试为每个“大”生命周期方法添加更多日志记录:
protected void onCreate(Bundle savedInstanceState);
protected void onStart();
protected void onRestart();
protected void onResume();
protected void onPause();
protected void onStop();
protected void onDestroy();
在跟踪生命周期不一致时,第一步应该是创建一个可扩展的日志记录活动。