snachmsm
snachmsm

Reputation: 19223

DiffUtil is passing wrong positions to areContentsTheSame method

I have a list with 25 items with stable ids. When user reach bottom of RecyclerView I'm fetching next 25 items and displaying whole list (50, 75 and so on).

DiffUtil.Callback is calling areItemsTheSame with values int oldItemPosition == int newItemPosition, I'm checking ids and they are the same, each call returns true (25 calls). So next call will be series of areContentsTheSame and I'm expecting also int oldItemPosition == int newItemPosition, BUT newItemPosition have always +13...

first calls of both methods looks like below (from own Log)

areItemsTheSame will return true, positions: 0 0
areItemsTheSame will return true, positions: 1 1
...
areContentsTheSame will return false, positions 0 13
areContentsTheSame will return false, positions 1 14
...

after these calls I'm logging changes in ListUpdateCallback:

onInserted pos: 25 count: 12
onChanged pos: 0 count: 25
onInserted pos: 0 count: 13

and after redrawing RecyclerView childs I'm seeing completely different items shown with change-type animation on every item (in fact I'm moved a bit to bottom to item 34+, visible items at once: 4)

the question is simple: why?!

Upvotes: 1

Views: 1366

Answers (1)

faranjit
faranjit

Reputation: 1627

See this link

This method is called only if areItemsTheSame(int, int) returns true for these items.

oldItemPosition The position of the item in the old list and newItemPosition is The position of the item in the new list which replaces the oldItem.

Upvotes: 1

Related Questions