18

I'm trying to check if some specific items are visible in the RecyclerView; But I couldn't implement that. Please help me to determine if my items are completely visible in the RecyclerView.

mrecylerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        LinearLayout ll = (LinearLayout) recyclerView.findChildViewUnder(dx, dy);
        if (ll != null) {
            TextureVideoView tvv = (TextureVideoView) ll.findViewById(R.id.cropTextureView);
        }
    }
});

I want to check if tvv view is completely visible within the mrecyclerView view.

Alex
  • 1,623
  • 1
  • 24
  • 48

2 Answers2

23

You could make some logic using the LayoutManager api to get the last completely visible item position in RecyclerView onScrolled method:

Kotlin
(vYourRecycler.layoutManager as LinearLayoutManager).findLastCompletelyVisibleItemPosition()
Java
((LinearLayoutManager) vYourRecycler.getLayoutManager()).findLastCompletelyVisibleItemPosition();

From the documentation:

Returns the adapter position of the last fully visible view. This position does not include adapter changes that were dispatched after the last layout pass.

Try to use it and notify the RecyclerView adapter to refresh.

Note: i don't know why you're using findViewById in onScrolled method, this work should be implemented in RecyclerView ViewHolder to improve performances

lubilis
  • 3,942
  • 4
  • 31
  • 54
  • 4
    findLastCompletelyVisibleItemPosition(); gives me last attached item, whether that is visible or not. – Tasneem Dec 22 '16 at 12:58
  • is this possible for `all visible item` instead of `last visible item`? – mochadwi Aug 07 '20 at 13:05
  • 2
    looks like to get `all visible item` is possible by iterating/looping between `first visible item index position` & `last visible item index position`, see here for more details: https://stackoverflow.com/a/35392153/3763032 – mochadwi Aug 07 '20 at 13:11
  • @mochadwi That's exactly what I was gonna say. – Dev4Life Aug 07 '23 at 04:19
3

you have to set LayoutManager for RecyclerView. if you are using most common LinearLayoutManager, then it 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