Mehrzad
Mehrzad

Reputation: 123

How to calculate how many items fit(visible) in screen at recyclerview?

I have a simple recycler view and their adapter my question is that how to calculate how many items fit(visible) in screen at recycler view? for example if my data list size is 30 and visible item in screen is 10 how can calculate number 10 (before loaded items in the list)?

Upvotes: 0

Views: 379

Answers (1)

snachmsm
snachmsm

Reputation: 19263

if you have RecyclerView then you have set LayoutManager, probably LinearLayoutManager, which have methods like findFirstCompletelyVisibleItemPosition() or partially visible, and similar for last also. you can calculate how much items are (completly or at least partially) visible on screen

edit: just read that you want these numbers BEFORE any drawing... thats a game changer, you probably should measure screen or layout and item size by self. making it easier: maybe it may be fixed height? or make a "dirty hack", populate RecyclerView with dummy data and draw INVISIBLE items just for measuring, then measure like above, and finally set "real" data for RecyclerView

Upvotes: 1

Related Questions