Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Get row from RecyclerView.findViewHolderForAdapterPosition()

I need to get a reference to a specific row within my recyclerView.

I used to do it like this

View row = recyclerView.childAt(position);

since the childAt() method is based on the ViewGroup, once the list is scrolled, the positions are messed up.

I read here I had to use recyclerView.findViewHolderForAdapterPosition() which returns a ViewHolder.

How do I get from the ViewHolder to the actual View I need to work with?

like image 721
Daniele Avatar asked Sep 01 '25 16:09

Daniele


2 Answers

Your should use findViewByPosition

findViewByPosition(int position)

Finds the view which represents the given adapter position.

View row = recyclerView.getLayoutManager().findViewByPosition(YourPosition);
like image 55
AskNilesh Avatar answered Sep 04 '25 21:09

AskNilesh


You can get the view from the ViewHolder using its public itemView member variable:

RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(position);
final View itemView = viewHolder.itemView;
like image 44
Markus Penguin Avatar answered Sep 04 '25 19:09

Markus Penguin