Explain Codes LogoExplain Codes Logo

Recyclerview - Get view at particular position

java
recyclerview
viewholder
adapterposition
Nikita BarsukovbyNikita Barsukov·Oct 19, 2024
TLDR

To fetch a view situated at a definitive position in a RecyclerView, utilize the .findViewByPosition() method from your layout manager:

RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); View specificView = layoutManager.findViewByPosition(n); // n is your desired position if (specificView != null) { // Note here - the view is now ready to be dazzled up! }

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:

RecyclerView.ViewHolder holder = recyclerView.findViewHolderForAdapterPosition(n); if (holder != null) { // Do the type-casting dance if working with a custom ViewHolder YourViewHolder yourViewHolder = (YourViewHolder) holder; // Now let's jazz things up with yourViewHolder }

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:

recyclerView.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() { @Override public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { View myView = rv.findChildViewUnder(e.getX(), e.getY()); if (myView != null && gestureDetector.onTouchEvent(e)) { // YES! I got the required position. Time for some action!! int position = rv.getChildAdapterPosition(myView); return true; } return false; } });

Image rotation and view updates

Fiddling with image rotation or similar fun requiring UI changes? Do this to update ViewHolder's itemView correctly:

yourViewHolder.itemView.setRotation(rotationAngle); // rotationAngle is the new angle for view rotation

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

public class YourViewHolder extends RecyclerView.ViewHolder { // ... public void manageData(int position) { int adapterPosition = getAdapterPosition(); int layoutPosition = getLayoutPosition(); // Now the position tracking game is on point! } }

During data binding

yourAdapter.bindViewHolder(yourViewHolder, position);

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:

linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); recyclerView.setLayoutManager(linearLayoutManager); recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() { @Override public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) { View childView = rv.findChildViewUnder(e.getX(), e.getY()); if (childView != null) { ImageView clickedPic = childView.findViewById(R.id.pic_id); enlargeImage(clickedPic); return true; } return false; } // ... });