简体   繁体   English

如何使用 ListAdapter 更新回收站视图?

[英]How to update recycler view using ListAdapter?

I'm currently using RecyclerView ListAdapter.我目前正在使用 RecyclerView ListAdapter。

I would like to know what is the equivalent of notifyDataSetChanged or how to update the whole list after adding a value/record?我想知道notifyDataSetChanged的​​等价物是什么,或者在添加值/记录后如何更新整个列表?

I am using this method currently, but it's not updating if it's not refreshed.我目前正在使用这种方法,但是如果不刷新它就不会更新。

Initializing the record初始化记录

mList = new ArrayList<>();

mList.add(new ModelClass();

mAdapter.submitList(mList);

Some code after creating a new record创建新记录后的一些代码

mAdapter.submitList(null)

mAdapter.submitList(modifiedList)

I'm thinking of going back to RecyclerView.Adapter<VH> and notifyDataSetChanged() so that my problems are solved.我正在考虑回到RecyclerView.Adapter<VH>notifyDataSetChanged()以便解决我的问题。 What do you think?你怎么看?

There are multiple ways to achieve same.有多种方法可以实现相同的目标。
First way:第一种方式:

 adapter.notifyItemInserted(itemIndex);

Here, itemIndex is the position in the array where you want to add a new element.这里, itemIndex是数组中要添加新元素的位置。 This will not refresh the entire list on the recycler view.这不会刷新回收站视图上的整个列表。
But will just add this element to that position.但只会将此元素添加到该位置。

Second Way:第二种方式:

 arrayData.addAll(insertIndex, items); adapter.notifyItemRangeInserted(itemIndex, items.size());

Third Way:第三种方式:

 adapter.notifyDataSetChanged();

So, like I have mentioned above there are multiple way of updating a list in recycler view所以,就像我上面提到的,有多种方法可以在回收站视图中更新列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM