Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get visible item in RecyclerView

How can I get a current item in RecycerView? In my use-case RV item takes all screen space, so there is only one item at a time. I've tried to google before, but nothing helpful was found.

The whole use case:

  1. UI. The bottom navigation has a button that should call a certain method (let's say a foo() method) in fragment

  2. this foo() method should get a visible item from the adapter and call a specific method in a presenter.

like image 557
P. Savrov Avatar asked Aug 21 '17 12:08

P. Savrov


2 Answers

use this in your code

    LinearLayoutManager mLayoutManager;
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);

and get your visible item using below code

mLayoutManager.findFirstVisibleItemPosition();
like image 169
Vinayak B Avatar answered Sep 20 '22 00:09

Vinayak B


You could also use an OnItemClickListener on your RecyclerView's ListAdapter to determine the current item. In that case the item needs to be clicked though.

like image 29
eheller Avatar answered Sep 20 '22 00:09

eheller