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

How to make specified item not update when notifyDataSetChanged in RecyclerView

我在 RecyclerView 中有一项在我调用 notifyDataSetChanged 时不需要更新。

但是我没有找到任何方法让指定的项目避免更新。可以这样做吗?


我需要这样做,因为该项目是 WebView,它加载了一个 html 页面,如果我调用 notifyDataSetChanged,该项目将闪烁。

我之所以post这道题是为了避免notifyDataSetChangedWebView的闪现(见)。在使用notifyItemRangeXXX方法失败后,我post这个问题。

但是在检查了您的答案和评论之后,似乎无法避免使用 notifyDataSetChanged 时更新。

您应该换个角度思考——哪些项目需要更新。要在数据更改后更新项目 UI,请使用方法 notifyitemchanged

notifyDataSetChanged 的目的是更新所有项目。如果您不想要这种行为,请不要使用它!

正确的方法是只更新您更改过的数据。有一些方法可以帮助您仅使所需数据无效:

notifyItemChanged(int position)
notifyItemInserted(int position)
notifyItemMoved(int fromPosition, int toPosition)
notifyItemRangeChanged(int positionStart, int itemCount)
notifyItemRangeRemoved(int positionStart, int itemCount)
notifyItemRemoved(int position)

是的,确实可以这样做。 notifyDataSetChanged() 通知 RecyclerView 数据集中的所有元素都已更改

因为您不希望发生这种情况并排除特定项目。

for(int i = 0; i< mAdapter.getItemCount(); i++){
    if(i != itemPositionToExclude){
       mAdapter.notifyItemChanged(i);
    }
}

因此,我们需要循环遍历适配器中的所有元素,并仅针对那些位置 which you do not want to exclude.

调用 notifyItemChanged()