'Know whether a view is completely visible in a recycler view from its adapter
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.
Solution 1:[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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |