Search code examples
androidkotlinandroid-recyclerviewandroid-diffutilsandroid-paging-3

DiffUtil.ItemCallback areItemsTheSame() method check items twice


I send request to /list.php?page=1 and display data in UI with Paging 3 component.

In first request, areItemsTheSame(new, old) method do nothing. No log in Logcat.

Then i refresh data with SwipeRefreshLayout and request sent same api (/list.php?page=1).

DiffUtil.ItemCallback areItemsTheSame(new, old) method check items twice. once in descending order then ascending order.

Logcat:

I: newItem: 3308 = oldItem: 3308 I: check: true
I: newItem: 3307 = oldItem: 3307 I: check: true
I: newItem: 3306 = oldItem: 3306 I: check: true
I: newItem: 3305 = oldItem: 3305 I: check: true
I: newItem: 3304 = oldItem: 3304 I: check: true
I: newItem: 3303 = oldItem: 3303 I: check: true
I: newItem: 3302 = oldItem: 3302 I: check: true
I: newItem: 3301 = oldItem: 3301 I: check: true
I: newItem: 3300 = oldItem: 3300 I: check: true
I: newItem: 3300 = oldItem: 3300 I: check: true
I: newItem: 3301 = oldItem: 3301 I: check: true
I: newItem: 3302 = oldItem: 3302 I: check: true
I: newItem: 3303 = oldItem: 3303 I: check: true
I: newItem: 3304 = oldItem: 3304 I: check: true
I: newItem: 3305 = oldItem: 3305 I: check: true
I: newItem: 3306 = oldItem: 3306 I: check: true
I: newItem: 3307 = oldItem: 3307 I: check: true
I: newItem: 3308 = oldItem: 3308 I: check: true

VacancyFragment: enter image description here

Adapter: enter image description here

ViewModel: enter image description here

DataSource: enter image description here


Solution

  • i think i found the answer.

    Myers's algorithm does not handle items that are moved so DiffUtil runs a second pass on the result to detect items that were moved.

    https://developer.android.com/reference/androidx/recyclerview/widget/DiffUtil