gpt4 book ai didi

android - 如何在 ExpandableListView 中保留滚动位置

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:20:31 25 4
gpt4 key购买 nike

在我的应用程序中,我有一个派生自 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);
}

两种解决方案均无效。我还尝试将这两种解决方案结合起来,当我编辑一个项目并返回列表时这会起作用,但当我更改手机方向时它不起作用

谁能提出实现我目标的好方法?

最佳答案

经过几次实验,我找到了一个令人满意的解决方案,它还保留了顶部可见项目的精细滚动位置。

事实上,需要保存和恢复三种不同的信息:列表状态(例如展开了哪些组)、第一个可见项目的索引及其精细滚动位置。

可惜的是,展开 ListView 的onSaveInstanceState方法好像只保存了第一个,其他两个需要单独保存。这与不可展开的 ListView 不同,其中 onSaveInstanceState 方法似乎保存了正确恢复列表状态和位置所需的所有信息(关于此主题,请参阅 Maintain/Save/Restore scroll position when returning to a ListView)。

这是代码 fragment ;在 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 设备上运行良好。

关于android - 如何在 ExpandableListView 中保留滚动位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5713585/

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