Xamarin.Android 中的 DiffUtil
DiffUtil in Xamarin.Android
这里是初级开发者所以请玩得开心:)
我的应用程序使用 RecyclerView 来显示从服务器返回的项目列表。适配器和刷新工作正常但是,应用程序 hangs/freezes 暂时当 updating/refreshing 列表。
我相信它在点击 NotifyDataSetChanged() 时会冻结,因为这会重绘列表中的所有内容(列表中可能有数百个项目)。在线查看后,似乎 DiffUtil 可能正是我所追求的,但我找不到 Xamarin.Android 的任何文档或教程,只是基于 Java 的常规 Android 而我不知道对任何一种语言的理解都不足以翻译它。
如果有人能指出我正确的方向,将不胜感激!
这对我来说也很新鲜,我以前见过。刚才直接试了一下,半小时后就成功了。
其中一些来自这里:https://medium.com/@iammert/using-diffutil-in-android-recyclerview-bdca8e4fbb00
基本上它说的是:
你的数据结构有 2 个不同的点(List
、IEnumerable
等...)听起来你已经有了,所以很好。
有一个 DiffUtil.Callback
class,您将在其中传递旧数据和新数据,此 class 将一个与另一个进行比较。
有一个方法可以将更新与您的实用程序一起发送 class。虽然 post 有一点不对,因为他没有更新旧数据。但如果你这样做了,那么它就必须像我一样工作。
如果您有任何疑问或 运行 遇到问题,请告诉我。
阅读 VideoLAN 的这篇文章后,我能够在 Xamarin.Android 中使用 DiffUtil:https://geoffreymetais.github.io/code/diffutil/。他解释得很好,他的项目中的示例非常有用。
下面是我的实施的 "universal" 版本。我建议在实施您自己的回调之前阅读每个 override
调用的作用(请参阅上面的 link)。相信我,这很有帮助!
回调:
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 在片段中工作
- DiffUtil 可以更新空列表(即不需要预先存在的数据)
- 动画由系统处理(即您不必自己添加)
- 调用
DispatchUpdatesTo(yourRecyclerAdapter)
的方法不必在你的适配器中,它可以在你的activity或片段 中
这里是初级开发者所以请玩得开心:)
我的应用程序使用 RecyclerView 来显示从服务器返回的项目列表。适配器和刷新工作正常但是,应用程序 hangs/freezes 暂时当 updating/refreshing 列表。
我相信它在点击 NotifyDataSetChanged() 时会冻结,因为这会重绘列表中的所有内容(列表中可能有数百个项目)。在线查看后,似乎 DiffUtil 可能正是我所追求的,但我找不到 Xamarin.Android 的任何文档或教程,只是基于 Java 的常规 Android 而我不知道对任何一种语言的理解都不足以翻译它。
如果有人能指出我正确的方向,将不胜感激!
这对我来说也很新鲜,我以前见过。刚才直接试了一下,半小时后就成功了。
其中一些来自这里:https://medium.com/@iammert/using-diffutil-in-android-recyclerview-bdca8e4fbb00
基本上它说的是:
你的数据结构有 2 个不同的点(
List
、IEnumerable
等...)听起来你已经有了,所以很好。有一个
DiffUtil.Callback
class,您将在其中传递旧数据和新数据,此 class 将一个与另一个进行比较。有一个方法可以将更新与您的实用程序一起发送 class。虽然 post 有一点不对,因为他没有更新旧数据。但如果你这样做了,那么它就必须像我一样工作。
如果您有任何疑问或 运行 遇到问题,请告诉我。
阅读 VideoLAN 的这篇文章后,我能够在 Xamarin.Android 中使用 DiffUtil:https://geoffreymetais.github.io/code/diffutil/。他解释得很好,他的项目中的示例非常有用。
下面是我的实施的 "universal" 版本。我建议在实施您自己的回调之前阅读每个 override
调用的作用(请参阅上面的 link)。相信我,这很有帮助!
回调:
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 在片段中工作
- DiffUtil 可以更新空列表(即不需要预先存在的数据)
- 动画由系统处理(即您不必自己添加)
- 调用
DispatchUpdatesTo(yourRecyclerAdapter)
的方法不必在你的适配器中,它可以在你的activity或片段 中