【发布时间】:2016-10-25 02:22:37
【问题描述】:
我最初关注the accepted answer in this previously posted question,了解如何保存和恢复 RecyclerView 的布局管理器。
我的 RecyclerView 在 Activity B 中。[注意:Activity A 将启动 Activity B,通过 Intent 传输数据。]
当用户在 Activity B 中单击视图时,会启动 Activity C。一旦 Activity C 关闭并销毁,Activity B 就可见了,我可以看到我在 RecyclerView 中恢复的所有数据。
问题: 但是,如果我离开我的应用程序(即按下 Home 键),它会被推到后台。当我的应用程序恢复时,再次调用 onCreate()。 LayoutManager 的状态正在保存,但是我的数据都没有显示在 RecyclerView 中。
我可以说我的 LayoutManager 状态在经过一些调试后被保存了。从后台恢复应用后再次调用 onCreate() 时显示如下:
Saved Instance State = Bundle[{ActivityB - Linear Layout State=android.support.v7.widget.LinearLayoutManager$SavedState@6358f8f, android:viewHierarchyState=Bundle[mParcelledData.dataSize=1296]}]
经过数小时的尝试,我不确定如何解决此问题。
我的代码:
public class ActivityB extends AppCompatActivity {
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
private Parcelable linearLayoutState;
private RecyclerAdapter adapter;
private Button more;
private String id;
private List<VideoItem> list;
private List<VideoItem> newList;
private final String LINEARLAYOUT_STATE_KEY = "Activity B - Linear Layout State";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blayout);
// Retrieving data from Activity A
Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("bundle");
someClass.innerClass class = bundle.getParcelable("data");
id = class.getID();
list = class.getList();
newList = new ArrayList<>();
// Instantiating Recycler View
recyclerView = (RecyclerView)findViewById(R.id.activityb_recyclerview);
recyclerView.setNestedScrollingEnabled(false);
linearLayoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(linearLayoutManager);
adapter = new RecyclerAdapter();
recyclerView.setAdapter(adapter);
moreButton = (Button)findViewById(R.id.activityb_more);
moreButton.setOnClickListener(new fetchMoreClickListener());
}
@Override
public void onResume(){
if (linearLayoutState != null) {
linearLayoutManager.onRestoreInstanceState(linearLayoutState);
} else {
// If no layout state is found, I need to populate my
// adapter.
// Here I am simply cloning my list again, to keep the original
// list intact. generateSubset() removes items from the cloned list
// to create a smaller subset that is added to the adapter.
for (ListItem item : list){
newList.add(new ListItemi(item));
}
adapter.addItems(generateSubset(0));
}
super.onResume();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
linearLayoutState = linearLayoutManager.onSaveInstanceState();
outState.putParcelable(LINEARLAYOUT_STATE_KEY, linearLayoutState);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
linearLayoutState = savedInstanceState.getParcelable(LINEARLAYOUT_STATE_KEY);
}
其他活动生命周期方法尚未被覆盖。我没有收到任何错误消息。只有我存储在我的视图中的数据不再显示在 RecyclerView 中。
编辑 1:
这是我的 RecyclerAdapter 代码:
公共类 RecyclerAdapter 扩展 RecyclerView.Adapter{ 现在私有静态日期; 私有列表列表;
public static class ItemHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
private Context context;
private String key;
private Bundle itemBundle;
private SimpleDraweeView photo;
private TextView title;
public ItemHolder(Context context, View view){
super(view);
this.context = context;
this.key = null;
this.itemBundle = null;
photo = (SimpleDraweeView)view.findViewById(R.id.itemholder_photo);
title = (TextView)view.findViewById(R.id.itemholder_title);
view.setOnClickListener(this);
}
public void bindData(Item item){
if (key == null){ key = item.getID(); }
if (itemBundle == null){
itemBundle = new Bundle();
itemBundle.setClassLoader(Item.class.getClassLoader());
itemBundle.putParcelable("data", item);
}
photo.setImageURI(Uri.parse(item.getPhotoURL()));
}
@Override
public void onClick(View view) {
Intent intent = new Intent(context, ActivityB.class);
intent.putExtra("bundle", itemBundle);
context.startActivity(intent);
}
}
public RecyclerAdapter(){
this.list = new ArrayList<>();
this.now = new Date();
}
public RecyclerAdapter(ArrayList<VideoItem> list){
this.list = list;
this.now = new Date();
this.notifyItemRangeInserted(0, list.size());
}
@Override
public RecyclerAdapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
View recyclerItem = LayoutInflater.from(context)
.inflate(R.layout.itemholder_item, parent, false);
return new ItemHolder(context, recyclerItem);
}
@Override
public void onBindViewHolder(RecyclerAdapter.ItemHolder holder, int position) {
Item item = list.get(position);
holder.bindData(item);
}
private static Date getNow(){ return now; }
private static void updateNow(){ now = new Date(); }
@Override
public int getItemCount() {
return list.size();
}
public void addItem(Item item){
list.add(item);
this.notifyItemInserted(list.size() - 1);
}
public void addItems(ArrayList<Item> items){
int oldSize = list.size();
list.addAll(items);
this.notifyItemRangeInserted(oldSize, items.size());
}
}
【问题讨论】:
-
你能发布你的 RecyclerAdapter 吗?
-
基本上,如果我没看错的话,你只是在恢复 LayoutManager 的状态,而不是在恢复 Adapter 的数据。因此,当您重新打开应用程序时,会创建一个带有空数据集的适配器并将其绑定到您的 RecyclerView。您应该做的是为 Adapter 创建一个构造函数,该构造函数接受一个包含要显示的数据的列表,并在您实例化它时传递该列表 (
adapter = new RecyclerAdapter(list);)。 -
@BenjaminScharbau RecyclerAdapter 添加在上面。所以最好也捆绑我的列表,如果 (savedInstanceState == null) 如果为 true,则调用 adapter = new RecyclerAdapter(),如果为 false,则调用 adapter = new RecyclerAdapter(savedList)?
-
这样,是的。但我实际上不明白为什么你首先用
new RecyclerAdapter()实例化它而不是直接将列表传递给用户。基本上,我大部分时间都在使用onResume()方法为适配器加载数据,并在该方法中实例化新适配器。我实际上从未在onCreate()通话期间设置它 -
你试过完全评论 OnResume 吗?我从不在简历中查看回收站视图,它一直运行良好。 @ByteSized
标签: java android android-activity activity-lifecycle