RecyclerView.ViewHolder

abstract class RecyclerView.ViewHolder

Known direct subclasses
FragmentViewHolder

ViewHolder implementation for handling Fragments.

GuidedActionsStylist.ViewHolder

ViewHolder caches information about the action item layouts' subviews.

ItemBridgeAdapter.ViewHolder

ViewHolder for the ItemBridgeAdapter.

LeanbackListPreferenceDialogFragment.ViewHolder

This class is deprecated.

Ue LeanbackListPreferenceDialogFragmentCompat.

LeanbackListPreferenceDialogFragmentCompat.ViewHolder

ViewHolder for each Item in the List.

PreferenceViewHolder

A RecyclerView.ViewHolder class which caches views associated with the default Preference layouts.


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

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

While LayoutParams belong to the 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

Public constructors

ViewHolder(itemView: View)

Public functions

Int

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

Int

This function is deprecated.

This method is confusing when adapters nest other adapters.

RecyclerView.Adapter<RecyclerView.ViewHolder!>?

Returns the Adapter that last bound this ViewHolder.

Int

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

Long

Returns The itemId represented by this ViewHolder.

Int
Int

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

Int

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

Int

This function is deprecated.

This method is deprecated because its meaning is ambiguous due to the async handling of adapter updates.

Boolean
Unit
setIsRecyclable(recyclable: Boolean)

Informs the recycler whether this item can be recycled.

String!

Public properties

View

Public constructors

ViewHolder

Added in 1.0.0
ViewHolder(itemView: View)

Public functions

getAbsoluteAdapterPosition

Added in 1.2.0
fun getAbsoluteAdapterPosition(): Int

Returns the Adapter position of the item represented by this ViewHolder with respect to the RecyclerView's Adapter. If the Adapter that bound this 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 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 notifyDataSetChanged, until the next layout pass, the return value of this method will be NO_POSITION.

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

Returns
Int

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

getAdapterPosition

Added in 1.0.0
Deprecated in 1.2.0
fun getAdapterPosition(): Int

getBindingAdapter

Added in 1.2.0
fun getBindingAdapter(): RecyclerView.Adapter<RecyclerView.ViewHolder!>?

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

Returns
RecyclerView.Adapter<RecyclerView.ViewHolder!>?

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

getBindingAdapterPosition

Added in 1.2.0
fun getBindingAdapterPosition(): Int

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

Note that this might be different than the 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 notifyDataSetChanged, until the next layout pass, the return value of this method will be NO_POSITION.

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

Returns
Int

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

getItemId

Added in 1.0.0
fun getItemId(): Long

Returns The itemId represented by this ViewHolder.

Returns
Long

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

getItemViewType

Added in 1.0.0
fun getItemViewType(): Int
Returns
Int

The view type of this ViewHolder.

getLayoutPosition

Added in 1.0.0
fun getLayoutPosition(): Int

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 getAbsoluteAdapterPosition or convertPreLayoutPositionToPostLayout.

Returns
Int

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

getOldPosition

Added in 1.0.0
fun getOldPosition(): Int

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
Int

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

getPosition

Added in 1.0.0
Deprecated in 1.0.0
fun getPosition(): Int

isRecyclable

Added in 1.0.0
fun isRecyclable(): Boolean
Returns
Boolean

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

See also
setIsRecyclable

setIsRecyclable

Added in 1.0.0
fun setIsRecyclable(recyclable: Boolean): Unit

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: Boolean

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

See also
isRecyclable

toString

fun toString(): String!

Public properties

itemView

Added in 1.0.0
val itemViewView