I'm having a problem with the following methods:
int firstVisibleItemPosition = gridLayoutManager.findFirstVisibleItemPosition();
int lastVisibleItemPosition = gridLayoutManager.findLastVisibleItemPosition();
My goal: save analytic data about what items the user have viewed.
In order to do that, I'm calling this methods in two different scenarios:
every time scrolling turned to "idle" state, and checking what are the visible items. in that case I'm getting the expected indexes.
when the
RecyclerView
become "visible" to the user. now that's when the problem starts. I would expect that when the fragment containing the recylcerView is passedonResume()
then callingfindLastVisibleItemPosition()
will return the visible items. but it return -1, in that case. I guess it have something to do with the asynchronous loading of the recyclerView + adapter internal items initialization relative to the fragment/activity lifecycle.
by postpone this code by a few milliseconds - findLastVisibleItemPosition()
returns the right indexes. but I don't want to postpone hard coded using handler + delayed runnable, because scheduling delayed runnable is a work-around to what I really want to do: detect when the recycler view finished inflating and drawing to the screen all the views that can feet inside of it..
so my questions are basically:
how can I detect when the RecyclerView finished all the initialzation/measuring/inflating and drawing of it child items that feet into screen? (before any user interaction..).
is there any reliable good practice way to know exactly what items within' the recycler view is really shown on screen?