Vijay Singh Chouhan
Vijay Singh Chouhan

Reputation: 303

android - Update new data only in recyclerview

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.

Upvotes: 2

Views: 1744

Answers (4)

Jeel Vankhede
Jeel Vankhede

Reputation: 12138

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

Upvotes: 2

user9506553
user9506553

Reputation: 1

you can use DiffUtil to refresh particular item changes. for reference check this https://proandroiddev.com/diffutil-is-a-must-797502bc1149

Upvotes: 0

Suraj Vaishnav
Suraj Vaishnav

Reputation: 8315

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.

Upvotes: 1

Murat Karagöz
Murat Karagöz

Reputation: 37604

Yes you can do that by specifying on which position you inserted the new data by calling

RecyclerView.Adapter#notifyItemInserted(int)

Upvotes: 0

Related Questions