Recyclerview - Get view at particular position
To fetch a view situated at a definitive position in a RecyclerView
, utilize the .findViewByPosition()
method from your layout manager:
Remember, specificView
may be null
if it's not on stage (visible on the screen).
Retrieving and updating views - ViewHolder style
Grabbing the ViewHolder
Need more than the View
? Say hello to .findViewHolderForAdapterPosition(int position)
for grabbing associated ViewHolder
:
Beware - holder
might be null
due to RecyclerView's zest for recycling non-visible items.
Updating views based on user action
For updating views due to user interactions, like a simple click or famed swipe, use your ninja coding skills to find the view and perform the update:
Image rotation and view updates
Fiddling with image rotation or similar fun requiring UI changes? Do this to update ViewHolder's itemView correctly:
And up for some fireworks? Trigger notifyDataSetChanged() or notifyItemChanged(int position) after view alterations to notify your adapter of the classic switcheroo.
View recycling and smart position management
Taming position retrieval with getAdapterPosition()
and getLayoutPosition()
Keen on keeping track of item positions amidst RecyclerView
's recycling party? Meet getAdapterPosition()
and getLayoutPosition()
:
Within ViewHolder class
During data binding
Horizontal image display and click-based enlargement
Enlarging an image upon clicking is no magic! Disperse images horizontally in a RecyclerView
with a LinearLayoutManager
set to horizontal and an OnClickListener
for the click effect:
Was this article helpful?