android - Xamarin.Android 中的 DiffUtil

标签 android xamarin android-recyclerview refresh freeze

这里是初级开发人员所以请玩​​得开心 :)

我的应用使用 RecyclerView 来显示从服务器返回的项目列表。适配器和刷新工作正常,但是,应用程序在更新/刷新列表时暂时挂起/卡住。

我相信当它点击 NotifyDataSetChanged() 时它会卡住,因为这会重绘列表中的所有内容(列表中可能有数百个项目)。在线查看后,似乎 DiffUtil 可能正是我所追求的,但我找不到 Xamarin.Android 的任何文档或教程,只是普通的基于 Java 的 Android,我对这两种语言的理解都不足以翻译它。

如果有人能指出我正确的方向,将不胜感激!

最佳答案

在阅读 VideoLAN 的这篇文章后,我能够让 DiffUtil 在 Xamarin.Android 中工作:https://geoffreymetais.github.io/code/diffutil/ .他解释得很好,他项目中的示例非常有用。

下面是我的实现的“通用”版本。我建议在实现您自己的回调之前阅读每个 override 调用的作用(请参阅上面的链接)。相信我,这很有帮助!

回调:

using Android.Support.V7.Util;
using Newtonsoft.Json;
using System.Collections.Generic;

class YourCallback : DiffUtil.Callback
{
    private List<YourItem> oldList;
    private List<YourItem> newList;

    public YourCallback(List<YourItem> oldList, List<YourItem> newList)
    {
        this.oldList = oldList;
        this.newList = newList;
    }

    public override int OldListSize => oldList.Count;

    public override int NewListSize => newList.Count;

    public override bool AreItemsTheSame(int oldItemPosition, int newItemPosition)
    {
        return oldList[oldItemPosition].Id == newList[newItemPosition].Id;
    }

    public override bool AreContentsTheSame(int oldItemPosition, int newItemPosition)
    {
        // Using JsonConvert is an easy way to compare the full contents of a data model however, you can check individual components as well
        return JsonConvert.SerializeObject(oldList[oldItemPosition]).Equals(JsonConvert.SerializeObject(newList[newItemPosition]));
    }
}

不要调用 NotifyDataSetChanged(),而是执行以下操作:

private List<YourItem> items = new List<YourItem>();

private void AddItems()
{
    // Instead of adding new items straight to the main list, create a second list
    List<YourItem> newItems = new List<YourItem>();
    newItems.AddRange(items);
    newItems.Add(newItem);

    // Set detectMoves to true for smoother animations
    DiffUtil.DiffResult result = DiffUtil.CalculateDiff(new YourCallback(items, newItems), true);

    // Overwrite the old data
    items.Clear();
    items.AddRange(newItems);

    // Despatch the updates to your RecyclerAdapter
    result.DispatchUpdatesTo(yourRecyclerAdapter);
}

可以通过使用自定义负载等进一步优化它,但这已经比在适配器上调用 NotifyDataSetChanged() 更重要了。

我花了一段时间尝试在网上找到的最后几件事:

  • DiffUtil 在 fragment 中工作
  • DiffUtil 可以更新一个空列表(即不需要预先存在的数据)
  • 动画由系统处理(即您不必自己添加)
  • 调用 DispatchUpdatesTo(yourRecyclerAdapter) 的方法不必在您的适配器中,它可以在您的 Activity 或 fragment 中

关于android - Xamarin.Android 中的 DiffUtil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52079910/

相关文章:

android - Android:合并Google Play服务位置和apache commons.io时无法合并dex

android - 从联系人列表启动应用程序

android - RecyclerView 适合屏幕时不要折叠 Toolbar

android - 如何在 Kotlin 中使用 `strings.xml` 从 Recycler Adapter 类访问 `getString()` 中的字符串

c# - Xamarin.iOS 与第三方框架绑定(bind)

java - 交错网格布局管理器 : Adding Margin makes the layout items pushed to the side

android - 与 firebase 11.8.0 和 google-services 插件 3.1.2 冲突

android - 如何访问 "include"布局内的按钮

android - 如何在应用程序被杀死时调试应用程序

c# - 如何使用 Xamarin 更改 ActionBar 菜单项的 Action View