在我的应用程序我有ExpandableListActivity派生的类。 当我滚动的内容,然后更换手机的方向或编辑一个项目,然后再返回到列表中,原来的列表中的位置并不preserved。 我已经尝试了两个解决方案,类似于我最近成功地与ListActivity派生类中使用。
in my app I have a class derived from ExpandableListActivity. When I scroll the contents, and then change phone orientation or edit an item and then go back to the list, the original list position is not preserved. I have tried two solutions, similar to those I have recently successfully used with ListActivity-derived classes.
解决方案1:
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:
Solution 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);
}
无论是解决方案的工作。我也尝试了两个解决方案相结合,而这个作品时,我编辑的项目,并返回到列表中,但是当我改变手机方向这是行不通的。
Neither solution works. I have also tried to combine the two solutions, and this works when I edit an item and go back to the list, but it does NOT work when I change the phone orientation
任何人都可以提出来实现我的目标的好方法?
Anyone can suggest a good way to achieve my goal?
经过几次实验,我得出一个满意的解决方案,这也preserves顶部可见项目的精细滚动位置。
After a few experiments I have come to a satisfactory solution, which also preserves the fine scroll position of the top visible item.
由于事实上,三个不同的部分信息需要保存和恢复:列表中的状态(例如哪些群体扩展),第一个可见项目的索引,并以其优良的滚动位置
As a matter of fact, three different pieces of information need to be saved and restored: the list state (e.g. which groups are expanded), the index of the first visible item, and its fine scroll position.
不幸的是,似乎只有第一个被保存由可膨胀列表视图的的onSaveInstanceState方法,所以其他两个需要分开保存。这是一个不可扩展列表视图,它出现的onSaveInstanceState方法,将所有需要正确地恢复状态列表和位置(关于这个问题的信息不同,请参阅的滚动到列表视图的位置)。
Unfortunately, it seems that only the first one is saved by the onSaveInstanceState method of the expandable list view, so the other two need to be saved separately. This is different from a non expandable list view, where it appears the onSaveInstanceState method saves all the information needed to properly restore state and position of the list (on this subject, see Scroll to a position in a listView).
下面是code段;在ExpandableListActivity派生类的顶部:
Here are the code snippets; on top of the ExpandableListActivity-derived class:
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;
于是,一些覆盖:
Then, some overrides:
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设备上。
This works fine on my Froyo device.