Luke C
Luke C

Reputation: 158

Detecting which item is showing in Recycler View

I have a Recycler View that shows one item at a time. I need to know what index that item is in relation to the Recycler View's other items.

I don't see any listener that i can add to the Recycler View to detect this.

How could this be achieved?

Upvotes: 0

Views: 576

Answers (1)

Amir Hossein Mirzaei
Amir Hossein Mirzaei

Reputation: 2375

you can use onScrollListener

 recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();

            //For finding first visible item position
            linearLayoutManager.findFirstCompletelyVisibleItemPosition();

            //For finding last visible item position
            linearLayoutManager.findLastCompletelyVisibleItemPosition() s();


        }
    });

Upvotes: 4

Related Questions