public abstract class

RecyclerView.ViewHolder

extends java.lang.Object

 java.lang.Object

↳androidx.recyclerview.widget.RecyclerView.ViewHolder

Subclasses:

ListItem.ViewHolder, RadioButtonListItem.ViewHolder, TextListItem.ViewHolder, SubheaderListItem.ViewHolder, SwitchListItem.ViewHolder, ActionListItem.ViewHolder, SeekbarListItem.ViewHolder, DrawerItemViewHolder, GuidedActionsStylist.ViewHolder, ItemBridgeAdapter.ViewHolder, LeanbackListPreferenceDialogFragmentCompat.ViewHolder, LeanbackListPreferenceDialogFragment.ViewHolder, SliceAdapter.SliceViewHolder, FragmentViewHolder, PreferenceViewHolder

Overview

A ViewHolder describes an item view and metadata about its place within the RecyclerView.

RecyclerView.Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive View results.

While RecyclerView.LayoutParams belong to the RecyclerView.LayoutManager, ViewHolders belong to the adapter. Adapters should feel free to use their own custom ViewHolder implementations to store data that makes binding view contents easier. Implementations should assume that individual item views will hold strong references to ViewHolder objects and that RecyclerView instances may hold strong references to extra off-screen item views for caching purposes

Summary

Fields
public final ViewitemView

Constructors
publicViewHolder(View itemView)

Methods
public final intgetAbsoluteAdapterPosition()

Returns the Adapter position of the item represented by this ViewHolder with respect to the RecyclerView's RecyclerView.Adapter.

public final intgetAdapterPosition()

public final RecyclerView.Adapter<RecyclerView.ViewHolder>getBindingAdapter()

Returns the RecyclerView.Adapter that last bound this RecyclerView.ViewHolder.

public final intgetBindingAdapterPosition()

Returns the Adapter position of the item represented by this ViewHolder with respect to the RecyclerView.Adapter that bound it.

public final longgetItemId()

Returns The itemId represented by this ViewHolder.

public final intgetItemViewType()

public final intgetLayoutPosition()

Returns the position of the ViewHolder in terms of the latest layout pass.

public final intgetOldPosition()

When LayoutManager supports animations, RecyclerView tracks 3 positions for ViewHolders to perform animations.

public final intgetPosition()

public final booleanisRecyclable()

public final voidsetIsRecyclable(boolean recyclable)

Informs the recycler whether this item can be recycled.

public java.lang.StringtoString()

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Fields

public final View itemView

Constructors

public ViewHolder(View itemView)

Methods

public final int getPosition()

Deprecated: This method is deprecated because its meaning is ambiguous due to the async handling of adapter updates. You should use RecyclerView.ViewHolder.getLayoutPosition(), RecyclerView.ViewHolder.getBindingAdapterPosition() or RecyclerView.ViewHolder.getAbsoluteAdapterPosition() depending on your use case.

See also: RecyclerView.ViewHolder.getLayoutPosition(), RecyclerView.ViewHolder.getBindingAdapterPosition(), RecyclerView.ViewHolder.getAbsoluteAdapterPosition()

public final int getLayoutPosition()

Returns the position of the ViewHolder in terms of the latest layout pass.

This position is mostly used by RecyclerView components to be consistent while RecyclerView lazily processes adapter updates.

For performance and animation reasons, RecyclerView batches all adapter updates until the next layout pass. This may cause mismatches between the Adapter position of the item and the position it had in the latest layout calculations.

LayoutManagers should always call this method while doing calculations based on item positions. All methods in RecyclerView.LayoutManager, RecyclerView.State, RecyclerView.Recycler that receive a position expect it to be the layout position of the item.

If LayoutManager needs to call an external method that requires the adapter position of the item, it can use RecyclerView.ViewHolder.getAbsoluteAdapterPosition() or RecyclerView.Recycler.convertPreLayoutPositionToPostLayout(int).

Returns:

Returns the adapter position of the ViewHolder in the latest layout pass.

See also: RecyclerView.ViewHolder.getBindingAdapterPosition(), RecyclerView.ViewHolder.getAbsoluteAdapterPosition()

public final int getAdapterPosition()

Deprecated: This method is confusing when adapters nest other adapters. If you are calling this in the context of an Adapter, you probably want to call RecyclerView.ViewHolder.getBindingAdapterPosition() or if you want the position as RecyclerView sees it, you should call RecyclerView.ViewHolder.getAbsoluteAdapterPosition().

Returns:

RecyclerView.ViewHolder.getBindingAdapterPosition()

public final int getBindingAdapterPosition()

Returns the Adapter position of the item represented by this ViewHolder with respect to the RecyclerView.Adapter that bound it.

Note that this might be different than the RecyclerView.ViewHolder.getLayoutPosition() if there are pending adapter updates but a new layout pass has not happened yet.

RecyclerView does not handle any adapter updates until the next layout traversal. This may create temporary inconsistencies between what user sees on the screen and what adapter contents have. This inconsistency is not important since it will be less than 16ms but it might be a problem if you want to use ViewHolder position to access the adapter. Sometimes, you may need to get the exact adapter position to do some actions in response to user events. In that case, you should use this method which will calculate the Adapter position of the ViewHolder.

Note that if you've called RecyclerView.Adapter.notifyDataSetChanged(), until the next layout pass, the return value of this method will be RecyclerView.NO_POSITION.

If the RecyclerView.Adapter that bound this RecyclerView.ViewHolder is inside another RecyclerView.Adapter (e.g. ConcatAdapter), this position might be different than RecyclerView.ViewHolder.getAbsoluteAdapterPosition(). If you would like to know the position that RecyclerView considers (e.g. for saved state), you should use RecyclerView.ViewHolder.getAbsoluteAdapterPosition().

Returns:

The adapter position of the item if it still exists in the adapter. RecyclerView.NO_POSITION if item has been removed from the adapter, RecyclerView.Adapter.notifyDataSetChanged() has been called after the last layout pass or the ViewHolder has already been recycled.

See also: RecyclerView.ViewHolder.getAbsoluteAdapterPosition(), RecyclerView.ViewHolder.getLayoutPosition()

public final int getAbsoluteAdapterPosition()

Returns the Adapter position of the item represented by this ViewHolder with respect to the RecyclerView's RecyclerView.Adapter. If the RecyclerView.Adapter that bound this RecyclerView.ViewHolder is inside another adapter (e.g. ConcatAdapter), this position might be different and will include the offsets caused by other adapters in the ConcatAdapter.

Note that this might be different than the RecyclerView.ViewHolder.getLayoutPosition() if there are pending adapter updates but a new layout pass has not happened yet.

RecyclerView does not handle any adapter updates until the next layout traversal. This may create temporary inconsistencies between what user sees on the screen and what adapter contents have. This inconsistency is not important since it will be less than 16ms but it might be a problem if you want to use ViewHolder position to access the adapter. Sometimes, you may need to get the exact adapter position to do some actions in response to user events. In that case, you should use this method which will calculate the Adapter position of the ViewHolder.

Note that if you've called RecyclerView.Adapter.notifyDataSetChanged(), until the next layout pass, the return value of this method will be RecyclerView.NO_POSITION.

Note that if you are querying the position as RecyclerView sees, you should use RecyclerView.ViewHolder.getAbsoluteAdapterPosition() (e.g. you want to use it to save scroll state). If you are querying the position to access the RecyclerView.Adapter contents, you should use RecyclerView.ViewHolder.getBindingAdapterPosition().

Returns:

The adapter position of the item from RecyclerView's perspective if it still exists in the adapter and bound to a valid item. RecyclerView.NO_POSITION if item has been removed from the adapter, RecyclerView.Adapter.notifyDataSetChanged() has been called after the last layout pass or the ViewHolder has already been recycled.

See also: RecyclerView.ViewHolder.getBindingAdapterPosition(), RecyclerView.ViewHolder.getLayoutPosition()

public final RecyclerView.Adapter<RecyclerView.ViewHolder> getBindingAdapter()

Returns the RecyclerView.Adapter that last bound this RecyclerView.ViewHolder. Might return null if this RecyclerView.ViewHolder is not bound to any adapter.

Returns:

The RecyclerView.Adapter that last bound this RecyclerView.ViewHolder or null if this RecyclerView.ViewHolder is not bound by any adapter (e.g. recycled).

public final int getOldPosition()

When LayoutManager supports animations, RecyclerView tracks 3 positions for ViewHolders to perform animations.

If a ViewHolder was laid out in the previous onLayout call, old position will keep its adapter index in the previous layout.

Returns:

The previous adapter index of the Item represented by this ViewHolder or RecyclerView.NO_POSITION if old position does not exists or cleared (pre-layout is complete).

public final long getItemId()

Returns The itemId represented by this ViewHolder.

Returns:

The item's id if adapter has stable ids, RecyclerView.NO_ID otherwise

public final int getItemViewType()

Returns:

The view type of this ViewHolder.

public java.lang.String toString()

public final void setIsRecyclable(boolean recyclable)

Informs the recycler whether this item can be recycled. Views which are not recyclable will not be reused for other items until setIsRecyclable() is later set to true. Calls to setIsRecyclable() should always be paired (one call to setIsRecyclabe(false) should always be matched with a later call to setIsRecyclable(true)). Pairs of calls may be nested, as the state is internally reference-counted.

Parameters:

recyclable: Whether this item is available to be recycled. Default value is true.

See also: RecyclerView.ViewHolder.isRecyclable()

public final boolean isRecyclable()

Returns:

true if this item is available to be recycled, false otherwise.

See also: RecyclerView.ViewHolder.setIsRecyclable(boolean)