override fun attachedToWindow() { adapterRef?.let { adapter -> adapterObserver = object : RecyclerView.AdapterDataObserver() { override fun onItemRangeInserted(positionStart: Int, itemCount: Int) { updateTrayVisibility(showTrayList(adapter)) }  override fun onItemRangeRemoved(positionstart: Int, itemcount: Int) { updateTrayVisibility(showTrayList(adapter)) } } adapterObserver?.let { adapter.registerAdapterDataObserver(it) } } }
When the [RecyclerView.Adapter] is attached to the window we register a data observer to always check whether to call [updateTrayVisibility].