Check if items are completely visible in the RecyclerView

Alireza picture Alireza · Sep 30, 2015 · Viewed 18.6k times · Source

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.

Answer

lubilis picture lubilis · Oct 18, 2015

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