首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DiffUtil in Xamarin.Android

DiffUtil in Xamarin.Android
EN

Stack Overflow用户
提问于 2018-08-29 14:27:58
回答 2查看 519关注 0票数 4

这里的初级开发者,所以请玩得好:)

我的应用程序使用RecyclerView来显示从服务器返回的项目列表。适配器和刷新工作正常,然而,应用程序挂起/冻结时,更新/刷新列表。

我确信,当它点击NotifyDataSetChanged()时,它就会结冰,因为这会重新绘制列表中的所有内容(列表中可能有数百个条目)。在网上查看之后,DiffUtil可能正是我想要的,但是我找不到Xamarin.Android的任何文档或教程,只是普通的基于Java的Android,而且我对这两种语言都不太懂。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-17 11:38:15

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

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

回调:

代码语言:javascript
复制
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(),而是执行以下操作:

代码语言:javascript
复制
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)的方法不必在适配器中,它可以在活动或片段中。
票数 5
EN

Stack Overflow用户

发布于 2018-08-29 18:31:47

这对我来说也是很新鲜的,我以前也见过。我刚才真的试过了,半个小时后就开始工作了。

所以其中一些来自于这里:https://medium.com/@iammert/using-diffutil-in-android-recyclerview-bdca8e4fbb00

它说的基本上是:

  1. 拥有数据结构的两个不同点(ListIEnumerable等)听起来你已经有了,所以这很好。
  2. 有一个DiffUtil.Callback类,您将在其中传递旧的和新的数据,该类将比较其中一个数据和另一个数据。
  3. 有一个方法,它将与您的实用工具类一起分派更新。虽然这篇文章的内容有点错误,因为他没有更新旧的数据。但如果你那么做了,它就会像对我一样起作用。

如果你有问题或遇到问题请告诉我。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52079910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档