[英]How to make specified item not update when notifyDataSetChanged in RecyclerView
I have a item in RecyclerView
which does not need to be updated when I call notifyDataSetChanged
. 我在
RecyclerView
有一个项目,当我调用notifyDataSetChanged
时不需要更新。
But I didn't find any methods to make the specified item avoid updated. 但是我没有找到使指定项目避免更新的任何方法。 Is it possible to do this ?
是否有可能做到这一点 ?
I need to do this because the item is a WebView
, it had loaded a html page, if I called notifyDataSetChanged
, this item will flash. 我需要执行此操作,因为该项目是
WebView
,它已经加载了html页面,如果我调用notifyDataSetChanged
,则该项目将闪烁。
The reason why I post this question is avoiding the flash of the WebView
when notifyDataSetChanged
(see this quesion ). 我发布此问题的原因是,当
notifyDataSetChanged
时避免了WebView
notifyDataSetChanged
(请参阅此问题 )。 After the failure of using notifyItemRangeXXX
methods, I post this question. 使用
notifyItemRangeXXX
方法失败后,我将发布此问题。
But after checking your answers and comments, it seems that it's impossible to avoid updating when using notifyDataSetChanged
. 但是在检查了您的答案和评论之后,似乎无法避免在使用
notifyDataSetChanged
时进行更新。
The purpose of notifyDataSetChanged
IS to update ALL items. notifyDataSetChanged
IS的目的是更新所有项目。 Don't use it if you don't want this behavior! 如果您不想要这种行为,请不要使用它!
The correct approach would be to only update the data you've changed. 正确的方法是仅更新您更改的数据。 There are some methods which will help you to only invalidate the desired data:
有一些方法可以帮助您仅使所需数据无效:
notifyItemChanged(int position)
notifyItemInserted(int position)
notifyItemMoved(int fromPosition, int toPosition)
notifyItemRangeChanged(int positionStart, int itemCount)
notifyItemRangeRemoved(int positionStart, int itemCount)
notifyItemRemoved(int position)
Yes, its actually possible to do so. 是的,这样做实际上是可能的。
notifyDataSetChanged()
notifies the RecyclerView that all the elements in the data set has changed . notifyDataSetChanged()
通知RecyclerView 数据集中的所有元素都已更改 。
As you do not want that to happen and exclude specific items. 由于您不希望发生这种情况,因此请排除特定的项目。
for(int i = 0; i< mAdapter.getItemCount(); i++){
if(i != itemPositionToExclude){
mAdapter.notifyItemChanged(i);
}
}
So, we need to loop through all the elements in the adapter and call notifyItemChanged()
only for those positions which you do not want to exclude
. 因此,我们需要遍历适配器中的所有元素,并仅对
which you do not want to exclude
位置调用notifyItemChanged()
。
You should think in different way - which items need to be updated. 您应该以不同的方式思考-哪些项目需要更新。 To update UI of item, after data has changed use method notifyitemchanged
要更新项目的用户界面,请在数据更改后使用方法notifyitemchanged
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.