问题描述
所以我有一个 RecyclerView
的活动,我想通过按下具有 的按钮来更改
在活动中.RecyclerView
中每个项目的 TextView
>onClickListener()
So I have an activity with RecyclerView
and I want to change TextView
of every item in the RecyclerView
by pressing button that has onClickListener()
in the activity.
我想知道在性能方面什么更好:
I'm wondering what is better in terms of performance:
- 使用
notifyDataSetChanged
. - 使用循环条件像 int i 小于
List.size()
其中notifyItemChanged
将被调用几次.
- Use
notifyDataSetChanged
ones. - Use loop with condition like int i is less than
List.size()
wherenotifyItemChanged
would be called few times.
在这两种情况下,我都在 RecyclerView
Adapter 中创建了布尔变量,onBindViewHolder
使用它来了解如何更新项目.默认情况下它是假的,点击按钮后它变成真,所以 onBindViewHolder
以不同的方式更新项目.
In both cases I create boolean variable in RecyclerView
Adapter which is used by onBindViewHolder
to know how to update item. By default it's false and after button click it becomes true so onBindViewHolder
updates item in different way.
我也想知道这种方法是否适合.
Also I would like to know if this approach is suitable at all.
推荐答案
如果您只是更新视图的一部分,请使用 notifyItemRangeChanged()
或 notifyItemChanged()
而不是 notifyDataSetChanged()
.这里的区别在于结构变化与项目变化.这是在 android 开发人员 RecyclerView.Adapter
文档中找到的 此处.
If you are simply updating one part of the view, use the notifyItemRangeChanged()
or notifyItemChanged()
instead of notifiyDataSetChanged()
. The difference here has to do with structural changes vs item changes. This is on the android developers RecyclerView.Adapter
documentation found here.
这里是关于这两种更改之间差异的另一个花絮:
Here is another tidbit on the differences between the two types of changes:
有两种不同类别的数据更改事件,项目更改和结构性变化.项目更改是当单个项目具有其数据已更新,但没有发生位置变化.结构的更改是在数据中插入、删除或移动项目时设置.
这是取自上述页面,
如果你正在编写一个适配器,它总是会更有效地使用如果可以,更具体的更改事件.依靠notifyDataSetChanged() 作为最后的手段.
所以只是为了澄清使用 notifyDataSetChanged()
作为最后的手段,而是问问自己是否可以执行这些方法之一,以及是否可以使用它相反:
So just to clarify use notifyDataSetChanged()
as a last resort, and instead ask yourself if you can preform one of these methods instead, and if you can use it instead:
notifyItemChanged(int)
notifyItemInserted(int)
notifyItemRemoved(int)
notifyItemRangeChanged(int, int)
notifyItemRangeInserted(int, int)
notifyItemRangeRemoved(int, int)
这是有道理的,因为 notifyDataSetChanged()
几乎会尝试根据数据重新绘制所有内容,并且不会对其进行先前的假设,而其他方法只会寻找更改.这意味着适配器必须做更多不必要的工作.这就是 notifyDataSetChanged()
所做的:
which makes sense because notifyDataSetChanged()
will pretty much try to redraw everything based on the data and make no previous assumptions on it, while the other methods will just look for changes. That means the adapter has to do a lot more work that is not necessary. This is what notifyDataSetChanged()
does:
这个事件没有说明数据集发生了什么变化,强迫任何观察者假设所有现有的项目和结构可能不再有效.LayoutManagers 将被迫完全重新绑定并重新布局所有可见视图.
这对于使用增量或范围方法也很有意义,因为您正在更改文本,您需要获取每个新文本,当您这样做时,您应该告诉适配器您更改了它.现在,如果您单击按钮并获取所有新文本值,然后创建一个新列表或其他内容,然后调用重notifyDataSetChanged()
.
This also makes sense to use the incremental or range approach, because you are changing the text, you need to go get each new text and when you do that you should tell the adapter you changed it. Now, if you do a button click and get all new text values, and create a new list or something then call heavy notifyDataSetChanged()
.
这篇关于什么更好?循环中的 notifyDataSetChanged 或 notifyItemChanged ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!