0

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)?

Mehrzad
  • 93
  • 1
  • 6

1 Answers1

1

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

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • thanks for your help , height of recycler view is match_parent and layoutManger as you said is LinearLayoutManager @snachmsm – Mehrzad Mar 26 '23 at 22:52
  • excuse me can you show me an example that how to measure adapter item view? @snachmsm – Mehrzad Mar 27 '23 at 09:58
  • it's not easy task, check out [this SO](https://stackoverflow.com/questions/2827079/measuring-a-view-before-rendering-it) – snachmsm Mar 27 '23 at 13:34