Accessing a RecyclerView Item
Question
How to get access to single item in
recycler viewfrom code?
Short interview answer
Keep the item in the adapter’s data model and expose an explicit callback or selection event from its view holder. Use the adapter position only at the time of interaction, then look up the corresponding model item.
Detailed answer
The view is recyclable and should not become the source of truth. Bind the model identifier and report it through a callback:
holder.ItemView.Click += (_, _) =>
{
var position = holder.BindingAdapterPosition;
if (position != RecyclerView.NoPosition)
onItemSelected(items[position].Id);
};
The screen acts on the model identifier, not on the row view. Do not retain a row view or assume a displayed position stays valid after list updates.