android - RecyclerView: find last visible item immediately after view created -


i'm having problem following methods:

int firstvisibleitemposition = gridlayoutmanager.findfirstvisibleitemposition(); int lastvisibleitemposition = gridlayoutmanager.findlastvisibleitemposition(); 

my goal: save analytic data items user have viewed.

in order that, i'm calling methods in 2 different scenarios:

  • every time scrolling turned "idle" state, , checking visible items. in case i'm getting expected indexes.

  • when recyclerview become "visible" user. that's when problem starts. expect when fragment containing recylcerview passed onresume() calling findlastvisibleitemposition() return visible items. but return -1, in case. guess have asynchronous loading of recyclerview + adapter internal items initialization relative fragment/activity lifecycle.

by postpone code few milliseconds - findlastvisibleitemposition() returns right indexes. don't want postpone hard coded using handler + delayed runnable, because scheduling delayed runnable work-around want do: detect when recycler view finished inflating , drawing screen views can feet inside of it..

so questions basically:

  • how can detect when recyclerview finished initialzation/measuring/inflating , drawing of child items feet screen? (before user interaction..).

  • is there reliable practice way know items within' recycler view shown on screen?

use onchildattachlistener detect when recycler attach new holder. use delay mechanism in order send correct analitycs (\not hardcoded delaybut this:

... public void run(){   removecallbacks()   postdelay(sendanalytics(correctinformation),25); } 

the 25ms delay more reliable attaching ot onresume arbitrary.


Comments