3

I have a item in RecyclerView which does not need to be updated when I call 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.

The reason why I post this question is avoiding the flash of the WebView when notifyDataSetChanged (see this quesion). After the failure of using notifyItemRangeXXX methods, I post this question.

But after checking your answers and comments, it seems that it's impossible to avoid updating when using notifyDataSetChanged.

Community
  • 1
  • 1
L. Swifter
  • 3,179
  • 28
  • 52
  • 1
    if you don't want it update, dont change the datasource – Linh Jul 29 '16 at 08:53
  • If viable, you can also use `notifyItemChanged(pos)` for others except the item in question. – Shaishav Jul 29 '16 at 08:55
  • If you're usign RecyclerView.Adapter. You might want to consider notifyItemChanged(int position), notifyItemRangeChanged(int positionStart, int itemCount) methods – Keyfe Ang Jul 29 '16 at 08:56

3 Answers3

5

Yes, its actually possible to do so. notifyDataSetChanged() notifies the RecyclerView that all the elements in the data set has changed.

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.

Aritra Roy
  • 15,355
  • 10
  • 73
  • 107
3

The purpose of notifyDataSetChanged IS to update ALL items. 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)
dipdipdip
  • 2,326
  • 1
  • 21
  • 31
1

You should think in different way - which items need to be updated. To update UI of item, after data has changed use method notifyitemchanged

Divers
  • 9,531
  • 7
  • 45
  • 88