RecyclerView notifyItemRangeChanged not showing new data

I have a problem with RecyclerView#Adapter regarding notifyItemRangeChanged . It seems that if Adapter believes that it has a size of 0 from the last call to getItemCount , and then I call Adapter#notifyItemRangeChanged(0, 1) , the Adapter simply ignores the call (this does not result in, for example, a new element).

Is this the expected behavior?

+5
source share
1 answer

Is this the expected behavior?

The short answer is yes.

From the documents notifyDataSetChanged() (yes, I know, another method, but just referring to it here, as it explains the difference between element changes and structural changes ):

There are two different classes of data change events, element changes, and structural changes. Element changes - this is when one element has updated data, but no position changes have occurred. Structural changes are when elements are inserted, deleted, or moved within a dataset.

Now read the documentation for notifyItemRangeChanged() (my emphasis):

This is an element change event, not a structural change event. . This means that any data reflection in this position range is outdated and needs to be updated. Elements in a given range retain the same identity.

This should answer your question. You are making structural changes (i.e., notifyItemRangeChanged() an element), so notifyItemRangeChanged() not a suitable method to call. Instead, you should call notifyItemRangeInserted() (or its singular equivalent), which indicates a structural change.

+13
source

Source: https://habr.com/ru/post/1232212/


All Articles