android - Update new data only in recyclerview

Viewed 1316

I have 500 above records in the list and i am using pagination to load 10 records each time, animation applied to the recyclerview what i want to do is when i am using notifyDataChanged its refresh all items of recyclerview, due to this reason my applied animation not working properly as i want.

Any idea how to refresh only new data in the recyclerview so that all items of recyclerview will not change.

4 Answers

You can use this method to update only newly added data in adapter,

adapter.notifyItemRangeChanged(int positionStart, int itemCount);

or use

adapter.notifyItemRangeInserted(int positionStart, int itemCount);

here,

positionStart: is a starting position from where you've inserted data (list size for most cases when inserted at end of list)

itemCount: is number of items inserted to list

More from here


notifyItemRangeInserted

Notify any registered observers that the currently reflected itemCount items starting at positionStart have been newly inserted. The items previously located at positionStart and beyond can now be found starting at position positionStart + itemCount.

This is a structural change event. Representations of other existing items in the data set are still considered up to date and will not be rebound, though their positions may be altered.

positionStart: Position of the first item that was inserted

itemCount: Number of items inserted


notifyItemRangeChanged

Notify any registered observers that the itemCount items starting at position positionStart have changed. Equivalent to calling notifyItemRangeChanged(position, itemCount, null);.

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.

positionStart: Position of the first item that has changed

itemCount: Number of items that have changed


Different is that notifyItemRangeInserted treats old data as up to date

Use notifyItemRangeChanged() method, like if you have 10 items already and when you scroll down 10 new items added to the array then you should do:

notifyItemRangeChanged(10,array.size())  

Which is : notifyItemRangeChanged(int positionStart, int itemCount)

You should use notifyItemRangeChanged because it reset items only between given range. rather than notifyDataSetChanged which reset all the items of RecyclerView.

Related