I saw a lot of posts that show the currently entirely visible item in the recycler view but, from the activity. But, I want to know about that from the adapter. If the view is completely visible, I want to start playing a video and when it is hidden, I want to stop it.
Asked
Active
Viewed 657 times
0
-
Does this answer your question? [Check if items are completely visible in the RecyclerView](https://stackoverflow.com/questions/32862189/check-if-items-are-completely-visible-in-the-recyclerview) – a_local_nobody May 13 '22 at 10:15
-
No @a_local_nobody. I want to check it from the adapter itself. Not activity – Sambhav Khandelwal May 13 '22 at 10:20
1 Answers
1
you have to set LayoutManager
for RecyclerView
. if you are using most common LinearLayoutManager, then it's have some methods for your purpose:
- findFirstCompletelyVisibleItemPosition()
- findFirstVisibleItemPosition()
- findLastCompletelyVisibleItemPosition()
- findLastVisibleItemPosition()
There are also similar methods in StaggeredGridLayoutManager, e.g. findFirstVisibleItemPositions
And general way would be to use bare LayoutManager
and its isViewPartiallyVisible method, but this probably needs more your code for particular use case

snachmsm
- 17,866
- 3
- 32
- 74
-
Yes. I did see that. But, how can I do so from the adapter itself? – Sambhav Khandelwal May 13 '22 at 10:20
-
well, you may pass `LayoutManager` instance set to `RecyclerView` also to adapter in e.g. constructor. but I would do this in `Activity`/`Fragment`, as you also need some scroll listener, which would check every `10dp`s of scroll if first item was previously only "visible" and now became "completelyVisible" – snachmsm May 13 '22 at 10:22
-
btw. you "want to" do this inside adapter, but thats not a job for adapter itself. it is desired to produce list items only, not managing scrolling and visibility changes (thats job for `LayoutManager` and `RecyclerView`) – snachmsm May 13 '22 at 10:24
-
-
perhaps worth writing this as an answer [here](https://stackoverflow.com/questions/32862189/check-if-items-are-completely-visible-in-the-recyclerview) instead and just deleting this post – a_local_nobody May 13 '22 at 10:26