Android 使用DiffUtil的RecyclerView,防止在更改时向下滚动
我的Android 使用DiffUtil的RecyclerView,防止在更改时向下滚动,android,android-recyclerview,Android,Android Recyclerview,我的recyclerivev有问题,特别是滚动。 我有一些列表,它是实时更新的,一些项目被添加,一些项目被删除,所有的东西都按照一些参数排序。 因此,最初在列表中位于第一位的项可以更改其参数,排序后该参数将位于不同的位置 因此,例如,我的recyclerView主要关注初始项,并且在更改后,当某个项具有“更好”参数时,该参数将与该初始项一起更改位置 问题是,当我不滚动时,我想用“更好”参数关注新项目,但当我按触摸滚动时,我不想关注新项目(这样我的触摸不会因为滚动到列表上当前的第一个项目而中断)
recyclerivev
有问题,特别是滚动。
我有一些列表,它是实时更新的,一些项目被添加,一些项目被删除,所有的东西都按照一些参数排序。
因此,最初在列表中位于第一位的项可以更改其参数,排序后该参数将位于不同的位置
因此,例如,我的recyclerView
主要关注初始项,并且在更改后,当某个项具有“更好”参数时,该参数将与该初始项一起更改位置
问题是,当我不滚动时,我想用“更好”参数关注新项目,但当我按触摸滚动时,我不想关注新项目(这样我的触摸不会因为滚动到列表上当前的第一个项目而中断)
因此,我不想在我的recyclerView
数据每次更改后强制执行此代码:
recyclerView.scrollToPosition(0);
因为正如我所说的,当我触摸我的recyclerView
列表并向下查看其他项目时,我会被这个滚动条打断,同时我的列表会发生变化
有没有办法做到这一点
具体来说,我正在使用DiffUtil中的DiffCallback来支持当前recyclerView列表发生更改时的动画-它将旧列表与另一个新列表进行比较,并应用所有需要的动画和通知(添加、删除、更改位置的项目)。所以我从不打电话
notifyDataSetChanged
或者诸如此类的事情
这是我的DiffUtil回调:
public static class DevicesDiffCallback extends DiffUtil.Callback{
List<DeviceInfo> oldDevices;
List<DeviceInfo> newDevices;
public DevicesDiffCallback(List<NexoDeviceInfo> newDevices, List<NexoDeviceInfo> oldDevices) {
this.newDevices = newDevices;
this.oldDevices = oldDevices;
}
@Override
public int getOldListSize() {
return oldDevices != null ? oldDevices.size() : 0;
}
@Override
public int getNewListSize() {
return newDevices != null ? newDevices.size() : 0;
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return oldDevices.get(oldItemPosition).getNexoIdentifier().getSerialNumber().equals(newDevices.get(newItemPosition).getNexoIdentifier().getSerialNumber());
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return oldDevices.get(oldItemPosition).equals(newDevices.get(newItemPosition));
}
@Override
public Object getChangePayload(int oldItemPosition, int newItemPosition) {
return super.getChangePayload(oldItemPosition, newItemPosition);
}
}
公共静态类DevicesDiffCallback扩展了DiffUtil.Callback{
列出旧设备;
列出新设备;
公用设备DiffCallback(列出新设备,列出旧设备){
this.newDevices=newDevices;
this.oldDevices=oldDevices;
}
@凌驾
public int getOldListSize(){
返回oldDevices!=null?oldDevices.size():0;
}
@凌驾
public int getNewListSize(){
返回newDevices!=null?newDevices.size():0;
}
@凌驾
公共布尔值areItemsTheSame(int-oldItemPosition,int-newItemPosition){
返回oldDevices.get(oldItemPosition).getNexoIdentifier().getSerialNumber().equals(newDevices.get(newItemPosition).getNexoIdentifier().getSerialNumber());
}
@凌驾
公共布尔值是相同的内容(int oldItemPosition,int newItemPosition){
返回oldDevices.get(oldItemPosition).equals(newDevices.get(newItemPosition));
}
@凌驾
公共对象getChangePayload(int-oldItemPosition、int-newItemPosition){
返回super.getChangePayload(oldItemPosition、newItemPosition);
}
}
我在我的适配器中这样设置,当我得到要填充的新数据列表并替换旧数据时:
public void setData(List<DeviceInfo> data) {
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DevicesDiffCallback(this.mData, data), false);
diffResult.dispatchUpdatesTo(this);
mData = data;
}
public void setData(列表数据){
DiffUtil.diffesult diffesult=DiffUtil.calculateDiff(新设备diffCallback(this.mData,data),false);
diffResult.dispatchUpdatesTo(本);
mData=数据;
}
我不确定这个答案,但我认为您调用DiffUtil
的代码不正确。尝试使用以下方法:
public void addItems(List<Recipe> recipeList) {
List<Recipe> newRecipeList = new ArrayList<>();
newRecipeList.addAll(this.recipeList);
newRecipeList.addAll(recipeList);
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new RecipeDiffUtilCallback(this.recipeList, newRecipeList));
this.recipeList.addAll(recipeList);
diffResult.dispatchUpdatesTo(this);
}
公共无效附加项(列表recipeList){
List newRecipeList=newArrayList();
newRecipeList.addAll(this.recipeList);
newRecipeList.addAll(recipeList);
DiffUtil.diffesult diffesult=DiffUtil.calculateDiff(新的RecipeDiffUtilCallback(this.recipeList,newRecipeList));
this.recipeList.addAll(recipeList);
diffResult.dispatchUpdatesTo(本);
}
所以当新数据出现时,您想滚动到列表顶部,而不是当用户手动滚动时?你能显示滚动到新项目的代码吗?我更新了我的问题。我实际上不做任何手动滚动。当我的新列表回调中有不同的顺序时,它会自动滚动到底部,并且初始值为的项目会“脱离屏幕”到底部,而recyclerView也会滚动到该位置。我不想滚动到底部,只想关注列表中当前或新的第一个位置,但不想在用户向下滚动时滚动到顶部。请检查,可能会有帮助。我不想在用户滚动时自动滚动到顶部。您想滚动到哪里汉克斯为我工作