# RecyclerView.ViewHolder

Artifact: [androidx.recyclerview:recyclerview](https://developer.android.com/jetpack/androidx/releases/recyclerview) [View Source](https://cs.android.com/search?q=file:androidx/recyclerview/widget/RecyclerView.java+class:androidx.recyclerview.widget.RecyclerView.ViewHolder) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

*** ** * ** ***


```
public abstract class RecyclerView.ViewHolder
```

<br />

Known direct subclasses [FragmentViewHolder](https://developer.android.com/reference/androidx/viewpager2/adapter/FragmentViewHolder), [GuidedActionsStylist.ViewHolder](https://developer.android.com/reference/androidx/leanback/widget/GuidedActionsStylist.ViewHolder), [ItemBridgeAdapter.ViewHolder](https://developer.android.com/reference/androidx/leanback/widget/ItemBridgeAdapter.ViewHolder), [LeanbackListPreferenceDialogFragment.ViewHolder](https://developer.android.com/reference/androidx/leanback/preference/LeanbackListPreferenceDialogFragment.ViewHolder), [LeanbackListPreferenceDialogFragmentCompat.ViewHolder](https://developer.android.com/reference/androidx/leanback/preference/LeanbackListPreferenceDialogFragmentCompat.ViewHolder), [PreferenceViewHolder](https://developer.android.com/reference/androidx/preference/PreferenceViewHolder)

|---|---|
| `https://developer.android.com/reference/androidx/viewpager2/adapter/FragmentViewHolder` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder` implementation for handling `https://developer.android.com/reference/androidx/fragment/app/Fragment`s. |
| `https://developer.android.com/reference/androidx/leanback/widget/GuidedActionsStylist.ViewHolder` | ViewHolder caches information about the action item layouts' subviews. |
| `https://developer.android.com/reference/androidx/leanback/widget/ItemBridgeAdapter.ViewHolder` | ViewHolder for the ItemBridgeAdapter. |
| `https://developer.android.com/reference/androidx/leanback/preference/LeanbackListPreferenceDialogFragment.ViewHolder` | **This class is deprecated.** Ue LeanbackListPreferenceDialogFragmentCompat. <br /> |
| `https://developer.android.com/reference/androidx/leanback/preference/LeanbackListPreferenceDialogFragmentCompat.ViewHolder` | ViewHolder for each Item in the List. |
| `https://developer.android.com/reference/androidx/preference/PreferenceViewHolder` | A `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder` class which caches views associated with the default `https://developer.android.com/reference/androidx/preference/Preference` layouts. |

*** ** * ** ***

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

`https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter` implementations should subclass ViewHolder and add fields for caching potentially expensive `https://developer.android.com/reference/android/view/View.html#findViewById(int)` results.

While `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.LayoutParams` belong to the `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.LayoutManager`, `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder` 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 fields |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/View.html` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#itemView()` |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#ViewHolder(android.view.View)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/View.html itemView)` |

| ### Public methods |
|---|---|
| `final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getAbsoluteAdapterPosition()()` Returns the Adapter position of the item represented by this ViewHolder with respect to the `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView`'s `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter`. |
| `final int` | `[getAdapterPosition](https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getAdapterPosition())()` **This method is deprecated.** This method is confusing when adapters nest other adapters. <br /> |
| `final @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter<https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder>` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getBindingAdapter()()` Returns the `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter` that last bound this `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder`. |
| `final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getBindingAdapterPosition()()` Returns the Adapter position of the item represented by this ViewHolder with respect to the `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter` that bound it. |
| `final long` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getItemId()()` Returns The itemId represented by this ViewHolder. |
| `final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getItemViewType()()` |
| `final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getLayoutPosition()()` Returns the position of the ViewHolder in terms of the latest layout pass. |
| `final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getOldPosition()()` When LayoutManager supports animations, RecyclerView tracks 3 positions for ViewHolders to perform animations. |
| `final int` | `[getPosition](https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getPosition())()` **This method is deprecated.** This method is deprecated because its meaning is ambiguous due to the async handling of adapter updates. <br /> |
| `final boolean` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#isRecyclable()()` |
| `final void` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#setIsRecyclable(boolean)(boolean recyclable)` Informs the recycler whether this item can be recycled. |
| `https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#toString()()` |

## Public fields

### itemView

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public final @NonNull View itemView
```

## Public constructors

### ViewHolder

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public ViewHolder(@NonNull View itemView)
```

## Public methods

### getAbsoluteAdapterPosition

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.2.0)

```
public final int getAbsoluteAdapterPosition()
```

Returns the Adapter position of the item represented by this ViewHolder with respect to the `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView`'s `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter`. If the `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter` that bound this `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder` is inside another adapter (e.g. `https://developer.android.com/reference/androidx/recyclerview/widget/ConcatAdapter`), this position might be different and will include the offsets caused by other adapters in the `https://developer.android.com/reference/androidx/recyclerview/widget/ConcatAdapter`.

Note that this might be different than the `https://developer.android.com/reference/androidx/recyclerview/widget/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 `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#notifyDataSetChanged()`, until the next layout pass, the return value of this method will be `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView#NO_POSITION()`.

Note that if you are querying the position as `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView` sees, you should use `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getAbsoluteAdapterPosition()` (e.g. you want to use it to save scroll state). If you are querying the position to access the `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter` contents, you should use `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getBindingAdapterPosition()`.

| Returns |
|---|---|
| `int` | The adapter position of the item from `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView`'s perspective if it still exists in the adapter and bound to a valid item. `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView#NO_POSITION()` if item has been removed from the adapter, `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#notifyDataSetChanged()` has been called after the last layout pass or the ViewHolder has already been recycled. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getBindingAdapterPosition()` |   |
| `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getLayoutPosition()` |   |

### getAdapterPosition

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0) Deprecated in [1.2.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.2.0)

```
public final int [getAdapterPosition](https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getAdapterPosition())()
```

> [!CAUTION]
> **This method is 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 `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getBindingAdapterPosition()` or if you want the position as `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView` sees it, you should call `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getAbsoluteAdapterPosition()`.

| Returns |
|---|---|
| `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getBindingAdapterPosition()` |

### getBindingAdapter

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.2.0)

```
public final @Nullable RecyclerView.Adapter<RecyclerView.ViewHolder> getBindingAdapter()
```

Returns the `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter` that last bound this `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder`. Might return `null` if this `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder` is not bound to any adapter.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter<https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder>` | The `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter` that last bound this `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder` or `null` if this `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder` is not bound by any adapter (e.g. recycled). |

### getBindingAdapterPosition

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.2.0)

```
public final int getBindingAdapterPosition()
```

Returns the Adapter position of the item represented by this ViewHolder with respect to the `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter` that bound it.

Note that this might be different than the `https://developer.android.com/reference/androidx/recyclerview/widget/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 `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#notifyDataSetChanged()`, until the next layout pass, the return value of this method will be `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView#NO_POSITION()`.

If the `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter` that bound this `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder` is inside another `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter` (e.g. `https://developer.android.com/reference/androidx/recyclerview/widget/ConcatAdapter`), this position might be different than `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getAbsoluteAdapterPosition()`. If you would like to know the position that `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView` considers (e.g. for saved state), you should use `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getAbsoluteAdapterPosition()`.

| Returns |
|---|---|
| `int` | The adapter position of the item if it still exists in the adapter. `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView#NO_POSITION()` if item has been removed from the adapter, `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#notifyDataSetChanged()` has been called after the last layout pass or the ViewHolder has already been recycled. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getAbsoluteAdapterPosition()` |   |
| `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getLayoutPosition()` |   |

### getItemId

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public final long getItemId()
```

Returns The itemId represented by this ViewHolder.

| Returns |
|---|---|
| `long` | The item's id if adapter has stable ids, `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView#NO_ID()` otherwise |

### getItemViewType

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public final int getItemViewType()
```

| Returns |
|---|---|
| `int` | The view type of this ViewHolder. |

### getLayoutPosition

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
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 `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.LayoutManager`, `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.State`, `https://developer.android.com/reference/androidx/recyclerview/widget/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 `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getAbsoluteAdapterPosition()` or `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Recycler#convertPreLayoutPositionToPostLayout(int)`.

| Returns |
|---|---|
| `int` | Returns the adapter position of the ViewHolder in the latest layout pass. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getBindingAdapterPosition()` |   |
| `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getAbsoluteAdapterPosition()` |   |

### getOldPosition

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
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 |
|---|---|
| `int` | The previous adapter index of the Item represented by this ViewHolder or `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView#NO_POSITION()` if old position does not exists or cleared (pre-layout is complete). |

### getPosition

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0) Deprecated in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public final int [getPosition](https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getPosition())()
```

> [!CAUTION]
> **This method is deprecated.**   
>
> This method is deprecated because its meaning is ambiguous due to the async handling of adapter updates. You should use `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getLayoutPosition()`, `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getBindingAdapterPosition()` or `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getAbsoluteAdapterPosition()` depending on your use case.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getLayoutPosition()` |   |
| `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getBindingAdapterPosition()` |   |
| `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getAbsoluteAdapterPosition()` |   |

### isRecyclable

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public final boolean isRecyclable()
```

| Returns |
|---|---|
| `boolean` | true if this item is available to be recycled, false otherwise. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#setIsRecyclable(boolean)` |   |

### setIsRecyclable

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
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 |
|---|---|
| `boolean recyclable` | Whether this item is available to be recycled. Default value is true. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#isRecyclable()` |   |

### toString

```
public String toString()
```