DiffUtil.ItemCallback areItemsTheSame方法检查项目两次

问题描述

我将请求发送到/list.php?page=1,并在具有分页3组件的UI中显示数据。

在第一个请求中, areItemsTheSame(新,旧)方法不执行任何操作。没有登录Logcat。

然后我使用SwipeRefreshLayout刷新数据,并请求发送相同的api(/list.php?page=1)。

DiffUtil.ItemCallback areItemsTheSame(新,旧)方法检查项目两次。一次降序,然后升序。

Logcat:

I:newItem:3308 = oldItem:3308 我:检查:是真的
I:newItem:3307 = oldItem:3307 我:检查:是真的
I:newItem:3306 = oldItem:3306 我:检查:是真的
I:newItem:3305 = oldItem:3305 我:检查:是真的
I:newItem:3304 = oldItem:3304 我:检查:是真的
I:newItem:3303 = oldItem:3303 我:检查:是真的
I:newItem:3302 = oldItem:3302 我:检查:是真的
I:newItem:3301 = oldItem:3301 我:检查:是真的
I:newItem:3300 = oldItem:3300 我:检查:是真的
I:newItem:3300 = oldItem:3300 我:检查:是真的
I:newItem:3301 = oldItem:3301 我:检查:是真的
I:newItem:3302 = oldItem:3302 我:检查:是真的
I:newItem:3303 = oldItem:3303 我:检查:是真的
I:newItem:3304 = oldItem:3304 我:检查:是真的
I:newItem:3305 = oldItem:3305 我:检查:是真的
I:newItem:3306 = oldItem:3306 我:检查:是真的
I:newItem:3307 = oldItem:3307 我:检查:是真的
I:newItem:3308 = oldItem:3308 我:检查:是

VacancyFragment:

enter image description here

适配器:

enter image description here

ViewModel:

enter image description here

数据源:

enter image description here

解决方法

我想我找到了答案。

Myers的算法无法处理移动的项目,因此DiffUtil 对结果进行第二次遍历以检测已移动的项目。

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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...