Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if items are completely visible in the RecyclerView

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.

like image 718
Alex Avatar asked Sep 30 '15 09:09

Alex


2 Answers

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

((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 for performance

like image 80
lubilis Avatar answered Oct 18 '22 08:10

lubilis


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

like image 2
snachmsm Avatar answered Oct 18 '22 06:10

snachmsm