Daniele
Daniele

Reputation: 4343

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?

Upvotes: 4

Views: 2802

Answers (2)

Markus Penguin
Markus Penguin

Reputation: 1661

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;

Upvotes: 2

AskNilesh
AskNilesh

Reputation: 69689

Your should use findViewByPosition

findViewByPosition(int position)

Finds the view which represents the given adapter position.

View row = recyclerView.getLayoutManager().findViewByPosition(YourPosition);

Upvotes: 9

Related Questions