在我的应用程序中,我有一个派生自 ExpandableListActivity 的类。
当我滚动内容,然后更改手机方向或编辑项目然后返回列表时,原始列表位置不会保留。
我尝试了两种解决方案,类似于我最近在 ListActivity 派生类中成功使用的解决方案。
解决方案一:
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
mListState = state.getParcelable(LIST_STATE);
}
protected void onResume() {
super.onResume();
loadData();
if (mListState != null)
getExpandableListView().onRestoreInstanceState(mListState);
}
protected void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
mListState = getExpandableListView().onSaveInstanceState();
state.putParcelable(LIST_STATE, mListState);
}
解决方案2:
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
mListPosition = state.getInt(LIST_STATE);
}
protected void onResume() {
super.onResume();
loadData();
getExpandableListView().setSelection(mListPosition);
}
protected void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
mListPosition = getExpandableListView().getFirstVisiblePosition();
state.putInt(LIST_STATE, mListPosition);
}
这两种解决方案都不起作用。我还尝试结合这两种解决方案,当我编辑项目并返回列表时,这有效,但当我更改手机方向时,它不起作用
任何人都可以建议一个实现我的目标的好方法吗?
经过几次实验,我得到了一个令人满意的解决方案,它还保留了顶部可见项目的精细滚动位置。
事实上,需要保存和恢复三条不同的信息:列表状态(例如,展开了哪些组)、第一个可见项的索引及其精细滚动位置。
不幸的是,可扩展列表视图的onSaveInstanceState方法似乎只保存了第一个,因此其他两个需要单独保存。这与不可展开的列表视图不同,在不可展开的列表视图中,onSaveInstanceState 方法保存了正确恢复列表状态和位置所需的所有信息(关于此主题,请参阅返回 ListView 时保持/保存/恢复滚动位置 https://stackoverflow.com/questions/3014089/scroll-to-a-position-in-a-listview).
这是代码片段;在 ExpandableListActivity 派生类之上:
private static final String LIST_STATE_KEY = "listState";
private static final String LIST_POSITION_KEY = "listPosition";
private static final String ITEM_POSITION_KEY = "itemPosition";
private Parcelable mListState = null;
private int mListPosition = 0;
private int mItemPosition = 0;
然后,一些覆盖:
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
// Retrieve list state and list/item positions
mListState = state.getParcelable(LIST_STATE_KEY);
mListPosition = state.getInt(LIST_POSITION_KEY);
mItemPosition = state.getInt(ITEM_POSITION_KEY);
}
protected void onResume() {
super.onResume();
// Load data from DB and put it onto the list
loadData();
// Restore list state and list/item positions
ExpandableListView listView = getExpandableListView();
if (mListState != null)
listView.onRestoreInstanceState(mListState);
listView.setSelectionFromTop(mListPosition, mItemPosition);
}
protected void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
// Save list state
ExpandableListView listView = getExpandableListView();
mListState = listView.onSaveInstanceState();
state.putParcelable(LIST_STATE_KEY, mListState);
// Save position of first visible item
mListPosition = listView.getFirstVisiblePosition();
state.putInt(LIST_POSITION_KEY, mListPosition);
// Save scroll position of item
View itemView = listView.getChildAt(0);
mItemPosition = itemView == null ? 0 : itemView.getTop();
state.putInt(ITEM_POSITION_KEY, mItemPosition);
}
这在我的 Froyo 设备上运行良好。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)