gpt4 book ai didi

android - 保存 RecyclerView 列表状态

转载 作者:太空狗 更新时间:2023-10-29 13:08:19 26 4
gpt4 key购买 nike

我有一个包含类对象列表的 recyclerView。目前,当我关闭我的应用程序时,recyclerView 中的所有列表都会丢失,并且在重新启动应用程序后,recyclerView 什么也不显示(无列表)。

即使在我的列表被关闭和销毁后,我应该使用什么以及如何保留列表?

public class Info  {
public String pName;
public String pContact;
public Character pGender;
public int pID;
public String tDateTime; // today's date and time
}

我将此类的对象存储在数组列表中以填充我的 recyclerview 适配器。

最佳答案

保存状态:

protected void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);

// Save list state
mListState = mLayoutManager.onSaveInstanceState();
state.putParcelable(LIST_STATE_KEY, mListState);
}

恢复状态:

protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);

// Retrieve list state and list/item positions
if(state != null)
mListState = state.getParcelable(LIST_STATE_KEY);
}

然后更新布局管理器:

@Override
protected void onResume() {
super.onResume();

if (mListState != null) {
mLayoutManager.onRestoreInstanceState(mListState);
}
}

关于android - 保存 RecyclerView 列表状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44581911/

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