简体   繁体   English

RecyclerView中的notifyDataSetChanged时如何使指定的项目不更新

[英]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.

相关问题 如何更新RecyclerView-notifyDataSetChanged() - How to Update RecyclerView - notifyDataSetChanged() 调用notifyDataSetChanged()时,RecyclerView不会更新或刷新 - RecyclerView does not update or refresh when calling notifyDataSetChanged() 在recyclerview项目上更改notifydataset单击 - notifydatasetchanged on recyclerview item click 如何使用notifyDataSetChanged以最简单的方式更新recyclerview - How to update recyclerview the simplest way using notifyDataSetChanged RecyclerView.Adapter notifyDataSetChanged()在显示最后一项时不起作用 - RecyclerView.Adapter notifyDataSetChanged() not working when is showing last item RecyclerView 项目中的 ProgressBar 未在 notifyDataSetChanged 上设置动画 - ProgressBar in RecyclerView item not animating on notifyDataSetChanged 如何在 notifyDataSetChanged 上禁用 recyclerview 滚动到顶部(第一项) - How to disable recyclerview scroll to top (1st item) on notifyDataSetChanged 如何在 notifyDataSetChanged 上为 recyclerView 设置动画? - How to animate recyclerView on notifyDataSetChanged? 以编程方式选择指定项目时,如何在RecyclerView中更改项目的属性? - How to change items' properties in RecyclerView when chosing a specified item programatically? 在RecyclerView中通过notifyDataSetChanged发布更新数据 - Issue on update data by notifyDataSetChanged in RecyclerView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM