首页 > 解决方案 > RecyclerView DiffUtil.dispatchUpdateTo 完成通知

问题描述

我目前正在使用 DiffUtil 类来计算更新并将更新发送到我的适配器ListUpdateCallback,如下所示:

   inner class NotificationCallback(private val onInsert: () -> Unit) : ListUpdateCallback {

            override fun onInserted(position: Int, count: Int) {
                notifyItemRangeInserted(position, count)
                Log.d("NotificationCallback", "onInserted position: $position count: $count")
                onInsert()
            }

            override fun onChanged(position: Int, count: Int, payload: Any?) {
                notifyItemRangeChanged(position, count)
                Log.d("NotificationCallback", "onChanged position: $position count: $count")
            }

            override fun onMoved(fromPosition: Int, toPosition: Int) {
                notifyItemMoved(fromPosition, toPosition)
                Log.d("NotificationCallback", "onMoved from: $fromPosition to: $toPosition")
            }

            override fun onRemoved(position: Int, count: Int) {
                notifyItemRangeRemoved(position, count)
                Log.d("NotificationCallback", "onRemoved position: $position count: $count")
            }
        }

正如您从这段代码中看到的那样,我正在使用相关方法更新适配器,但我只想在插入某些内容时运行一些代码。但是,这确实有效,因为可以(并且)在插入代码之后调用其他通知方法,这会导致插入某些内容时我正在运行的代码出现一些问题。理想情况下,我需要知道所有更新何时已发送到适配器,以便我可以在需要时运行相关代码。有没有这样的方法或方法可以知道这一点?

我目前的解决方案是用来postDelayed执行这段代码,并希望适配器更新在它运行时已经完成。不漂亮,也不理想!

标签: androidkotlinandroid-recyclerviewandroid-diffutils

解决方案


推荐阅读