如何使用Delegates.Observable获得新旧数据之间的区别?

时间:2018-07-19 08:15:42

标签: android kotlin delegates observable

获取差异而不是返回整个值以重绘UI会更好吗?

var collection: List<String> by 
Delegates.observable(emptyList()) { prop, old, new ->
    notifyDataSetChanged()    
}

是否可以提高效率?

1 个答案:

答案 0 :(得分:0)

您应该看看DiffUtil

  

DiffUtil是一个实用程序类,它可以计算两个列表之间的差异,并输出将第一个列表转换为第二个列表的更新操作列表。

     

DiffUtil使用Eugene W. Myers的差分算法来计算将一个列表转换为另一个列表的最小更新数。 Myers的算法无法处理已移动的项目,因此DiffUtil对结果进行第二次遍历以检测已移动的项目。

     

如果列表很大,此操作可能会花费大量时间,因此建议您在后台线程上运行此操作,

基本上,您必须使用两个列表来实现DiffUtil.Callback

data class MyPojo(val id: Long, val name: String)

class DiffCallback(
        private val oldList: List<MyPojo>,
        private val newList: List<MyPojo>
) : DiffUtil.Callback() {

    override fun getOldListSize() = oldList.size

    override fun getNewListSize() = newList.size

    override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return oldList[oldItemPosition].id == newList[newItemPosition].id
    }

    override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return oldList[oldItemPosition].name == newList[newItemPosition].name
    }

    override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
        // Implement method if you're going to use ItemAnimator
        return super.getChangePayload(oldItemPosition, newItemPosition)
    }
}

然后,您必须使用它来通知适配器。例如,您可以像这样在适配器中创建一个函数:

fun swap(items: List<myPojo>) {
    val diffCallback = ActorDiffCallback(this.items, items)
    val diffResult = DiffUtil.calculateDiff(diffCallback)

    this.items.clear()
    this.items.addAll(items)
    diffResult.dispatchUpdatesTo(this)
}

以您的情况为例,假设collection是适配器的成员:

var collection: List<String> by Delegates.observable(emptyList()) { prop, old, new ->
    val diffCallback = DiffCallback(old, new)
    val diffResult = DiffUtil.calculateDiff(diffCallback)
    diffResult.dispatchUpdatesTo(this)
}

一些参考文献: