RecyclerView DiffUtil.dispatchUpdateTo完成通知

时间:2019-05-22 07:12:37

标签: android kotlin android-recyclerview android-diffutils

我目前正在使用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执行此代码,并希望适配器更新在其运行时已完成。不漂亮,还是不理想!

0 个答案:

没有答案