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.

0

2 Answers 2

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

4
  • 4
    findLastCompletelyVisibleItemPosition(); gives me last attached item, whether that is visible or not.
    – Tasneem
    Commented Dec 22, 2016 at 12:58
  • is this possible for all visible item instead of last visible item?
    – mochadwi
    Commented Aug 7, 2020 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: stackoverflow.com/a/35392153/3763032
    – mochadwi
    Commented Aug 7, 2020 at 13:11
  • @mochadwi That's exactly what I was gonna say.
    – Dev4Life
    Commented Aug 7, 2023 at 4: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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.