相关疑难解决方法(0)

使用RecyclerView适配器更新数据的最佳方法

当我必须使用带有ListView的经典适配器时,我在ListView中更新我的数据,如下所示:

myAdapter.swapArray(data);

public swapArray(List<Data> data) {
  clear();
  addAll(data);
  notifyDataSetChanged();
}
Run Code Online (Sandbox Code Playgroud)

我想知道RecyclerView的最佳实践是什么.因为在RecyclerView适配器,你不能做clear,并addAllListView控件.

所以我尝试了一下notifyDataSetChanged,但它没有用.然后我尝试在我的视图上使用swapAdapter:

List<Data> data = newData;

MyRecyclerAdapter adapter = new MyRecyclerAdapter(data);

// swapAdapter on my recyclerView (instead of a .setAdapter like with a classic listView).
recyclerViewList.swapAdapter(adapter, false);
Run Code Online (Sandbox Code Playgroud)

但是使用这个最后的解决方案,我仍然需要创建我的适配器的新实例,我觉得它不是最好的解决方案.我应该能够在没有新的情况下更改我的数据MyRecyclerAdapter.

android listview android-adapter android-recyclerview

73
推荐指数
4
解决办法
10万
查看次数