fun <VH : RecyclerView.ViewHolder> RecyclerView.Adapter<out VH>.observeFirstInsert(block: () -> Unit) { val observer = object : RecyclerView.AdapterDataObserver() { override fun onItemRangeInserted(positionStart: Int, itemCount: Int) { block.invoke() unregisterAdapterDataObserver(this) } } registerAdapterDataObserver(observer) }
Observes the adapter and invokes the callback [block] only when data is first inserted to the adapter.