I have a RecyclerView.Adapter
together with a list of items. At some point I will make some changes to this list and then I will call notifyDataSetChanged()
(or another similar method). How do I know how many ViewHolders
will be created prior to making this call? Or how many times onBindViewHolder
will be called? Ideally I should know one of these numbers before calling this notify
method, but I could also manage with receiving a callback when the adapter finishes to make all the work (inflations, bindings). From my experiments, the adapter is not done with the work right after calling notifyDataSetChanged
.
I need to know how many items will be or were inflated. Ideally 'will be'. Some ideas on how I can achieve this?
UPDATE
Stumbled upon: Is there a callback for when RecyclerView has finished showing its items after I've set it with an adapter?, which gave me an answer to the second thing I asked: "I could also manage with receiving a callback when the adapter finishes to make all the work (inflations, bindings)."
I made some modifications to the accepted answer and the following Log
only displays after all the recycler's items were inflated (at least in my app):
RecyclerView(context).apply {
layoutManager = object: LinearLayoutManager(context, VERTICAL, false) {
override fun onLayoutCompleted(state: RecyclerView.State?) {
super.onLayoutCompleted(state)
val firstVisibleItemPosition = findFirstVisibleItemPosition()
val lastVisibleItemPosition = findLastVisibleItemPosition()
if(firstVisibleItemPosition == -1 || lastVisibleItemPosition == -1)
return
Log.d("RECYCLER", "all sync inflations done")
}
}
}