Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView notifyItemRangeChanged doesn't show new data

I am running into an issue with the RecyclerView#Adapter regarding notifyItemRangeChanged. It seems that if the Adapter thinks it has a size of 0 from the last call to getItemCount, and then I call Adapter#notifyItemRangeChanged(0, 1), the Adapter will simply ignore the call (it doesn't result in the new item being inserted, for example).

Is this the expected behavior?

like image 317
Jin Avatar asked Sep 24 '15 19:09

Jin


1 Answers

Is this the expected behavior?

Short answer: yes.

From the docs on notifyDataSetChanged() (yes, different method, I know, but just referencing it here since it explains the difference between item changes and structural changes):

There are two different classes of data change events, item changes and structural changes. Item changes are when a single item has its data updated but no positional changes have occurred. Structural changes are when items are inserted, removed or moved within the data set.

Now have a read through the documentation on notifyItemRangeChanged() (my emphasis):

This is an item change event, not a structural change event. It indicates that any reflection of the data in the given position range is out of date and should be updated. The items in the given range retain the same identity.

That should answer your question. You're making a structural change (that is, you're adding an item), hence notifyItemRangeChanged() is not the appropriate method to call. Instead, you should call notifyItemRangeInserted() (or its singular equivalent), which does indicate a structural change was made.

like image 80
MH. Avatar answered Sep 25 '22 21:09

MH.