使用DiffUtil的RecyclerView,防止在更改时滚动到底部 [英] RecyclerView using DiffUtil, prevent to scroll bottom on change

查看:43
本文介绍了使用DiffUtil的RecyclerView,防止在更改时滚动到底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的recyclerViev有问题,尤其是滚动问题. 我有一些列表,这些列表是实时更新的,添加了某些项目,删除了一些,所有内容都按某个参数排序. 因此,最初在列表中排在首位的项目可以更改其参数,该参数在排序后将位于不同的位置.

I have a problem with my recyclerViev, specifically with the scrolling. I have some list, which is updated in real time, some item is added, some removed, and everything is sorted by some parameter. So the item which was initially first on the list, can have its parameter changed, which will be in different position after the sorting.

例如,我的recyclerView专注于初始项目,并且在更改后,当某个具有更好"参数的项目正在更改该初始项目的位置.

So my recyclerView is for example focusing on the initial item, and after change, when some item has "better" parameter is changing position with that initial item.

问题是,我想在不滚动时使用更好"的参数来关注新项目,但是当我通过触摸滚动时,我不想关注它(因此我的触摸不会被打断)通过滚动到列表上的当前第一项).

Problem is, i want to focus on the new item, with "better" parameter when I'm not scrolling, but i don't want to focusing on it when i scroll by touch(so my touch will not be interrupted by scrolling to current first item on the list).

因此,我不想在recyclerView数据中的每次更改后都强制执行此代码:

So i don't want to force this code after every change in my recyclerView data:

recyclerView.scrollToPosition(0);

因为正如我所说,当我触摸recyclerView列表并向下浏览其他项目时,此滚动会打断我,同时我的列表也会发生变化.

because as i said, i will be interrupted by this scroll when i am touching my recyclerView list and go down to see other items and in the same time there will be a change in my list.

有没有办法做到这一点?

Is there a way to accomplish this?

具体来说,我使用的是 DiffUtil 中的 DiffCallback ,以在当前recyclerView列表发生更改时支持动画-它会将旧列表与另一个列表进行比较新列表并应用所有所需的动画和通知(添加,删除,更改位置的项目).所以我永远不会打电话

To be specific, i am using DiffCallback from the DiffUtil, to support animations when there is a change in my current recyclerView list - it compares the old list with another new list and apply all the wanted animations and notifications(item added, removed, changed position). So i never call

notifyDataSetChanged

或类似的东西

这是我的DiffUtil回调:

Here is my DiffUtil callback:

  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);
    }
}

当我得到要填充的新数据列表并替换旧数据时,我在适配器中将其设置为这样:

And i set it like this in my adapter, when i get the list of new data to be populated and replace the old data:

 public void setData(List<DeviceInfo> data) {
    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DevicesDiffCallback(this.mData, data), false);
    diffResult.dispatchUpdatesTo(this);

        mData = data;

}

推荐答案

我不确定这个答案,但是,我认为您调用DiffUtil的代码不合适.尝试使用此:

I'm not sure about this answer but, I think your code to call DiffUtil is not proper. Try using this :

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);
}

这篇关于使用DiffUtil的RecyclerView,防止在更改时滚动到底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆