# LoadStateAdapter

Artifact: [androidx.paging:paging-runtime](https://developer.android.com/jetpack/androidx/releases/paging) [View Source](https://cs.android.com/search?q=file:androidx/paging/LoadStateAdapter.kt+class:androidx.paging.LoadStateAdapter) Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/paging#3.0.0)

*** ** * ** ***


```
public abstract class LoadStateAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<@NonNull VH>
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [androidx.recyclerview.widget.RecyclerView.Adapter](https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter)\<@[NonNull](https://developer.android.com/reference/androidx/annotation/NonNull) VH\> ||
|   | ↳ | [androidx.paging.LoadStateAdapter](https://developer.android.com/reference/androidx/paging/LoadStateAdapter) |

*** ** * ** ***

Adapter for displaying a RecyclerView item based on `https://developer.android.com/reference/androidx/paging/LoadState`, such as a loading spinner, or a retry error button.

By default will use one shared `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#getItemViewType(int)` for all items.

By default, both `https://developer.android.com/reference/androidx/paging/LoadState.Loading` and `https://developer.android.com/reference/androidx/paging/LoadState.Error` are presented as adapter items, other states are not. To configure this, override `https://developer.android.com/reference/androidx/paging/LoadStateAdapter#displayLoadStateAsItem(androidx.paging.LoadState)`.

To present this Adapter as a header and or footer alongside your `https://developer.android.com/reference/androidx/paging/PagingDataAdapter`, see `https://developer.android.com/reference/androidx/paging/PagingDataAdapter#withLoadStateHeaderAndFooter(androidx.paging.LoadStateAdapter,androidx.paging.LoadStateAdapter)`, or use `https://developer.android.com/reference/androidx/recyclerview/widget/ConcatAdapter` directly to concatenate Adapters.

```kotlin
import androidx.paging.LoadState
import androidx.paging.LoadStateAdapter
import androidx.recyclerview.widget.RecyclerView

class LoadStateViewHolder(parent: ViewGroup, retry: () -> Unit) :
    RecyclerView.ViewHolder(
        LayoutInflater.from(parent.context).inflate(R.layout.load_state_item, parent, false)
    ) {
    private val progressBar: ProgressBar = itemView.findViewById(R.id.progress_bar)
    private val errorMsg: TextView = itemView.findViewById(R.id.error_msg)
    private val retry: Button =
        itemView.findViewById<Button>(R.id.retry_button).also {
            it.setOnClickListener { retry.invoke() }
        }

    fun bind(loadState: LoadState) {
        if (loadState is LoadState.Error) {
            errorMsg.text = loadState.error.localizedMessage
        }
        progressBar.visibility = toVisibility(loadState is LoadState.Loading)
        retry.visibility = toVisibility(loadState !is LoadState.Loading)
        errorMsg.visibility = toVisibility(loadState !is LoadState.Loading)
    }

    private fun toVisibility(constraint: Boolean): Int =
        if (constraint) {
            View.VISIBLE
        } else {
            View.GONE
        }
}

/**
 * Adapter which displays a loading spinner when `state = LoadState.Loading`, and an error
 * message and retry button when `state is LoadState.Error`.
 */
class MyLoadStateAdapter(private val retry: () -> Unit) :
    LoadStateAdapter<LoadStateViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState) =
        LoadStateViewHolder(parent, retry)

    override fun onBindViewHolder(holder: LoadStateViewHolder, loadState: LoadState) =
        holder.bind(loadState)
}
```

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/paging/PagingDataAdapter#withLoadStateHeaderAndFooter(androidx.paging.LoadStateAdapter,androidx.paging.LoadStateAdapter)` |   |
| `https://developer.android.com/reference/androidx/paging/PagingDataAdapter#withLoadStateHeader(androidx.paging.LoadStateAdapter)` |   |
| `https://developer.android.com/reference/androidx/paging/PagingDataAdapter#withLoadStateFooter(androidx.paging.LoadStateAdapter)` |   |

## Summary

| ### Public constructors |
|---|
| `<VH extends https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder> https://developer.android.com/reference/androidx/paging/LoadStateAdapter#LoadStateAdapter()()` |

| ### Public methods |
|---|---|
| `boolean` | `https://developer.android.com/reference/androidx/paging/LoadStateAdapter#displayLoadStateAsItem(androidx.paging.LoadState)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/LoadState loadState)` Returns true if the LoadState should be displayed as a list item when active. |
| `final int` | `https://developer.android.com/reference/androidx/paging/LoadStateAdapter#getItemCount()()` Returns the total number of items in the data set held by the adapter. |
| `final int` | `https://developer.android.com/reference/androidx/paging/LoadStateAdapter#getItemCount()()` |
| `final int` | `https://developer.android.com/reference/androidx/paging/LoadStateAdapter#getItemViewType(kotlin.Int)(int position)` Return the view type of the item at `position` for the purposes of view recycling. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/LoadState` | `https://developer.android.com/reference/androidx/paging/LoadStateAdapter#getLoadState()()` LoadState to present in the adapter. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter.StateRestorationPolicy` | `https://developer.android.com/reference/androidx/paging/LoadStateAdapter#getStateRestorationPolicy()()` |
| `int` | `https://developer.android.com/reference/androidx/paging/LoadStateAdapter#getStateViewType(androidx.paging.LoadState)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/LoadState loadState)` Override this method to use different view types per LoadState. |
| `abstract void` | `https://developer.android.com/reference/androidx/paging/LoadStateAdapter#onBindViewHolder(androidx.recyclerview.widget.RecyclerView.ViewHolder,androidx.paging.LoadState)(@https://developer.android.com/reference/androidx/annotation/NonNull VH holder, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/LoadState loadState)` Called to bind the passed LoadState to the ViewHolder. |
| `final void` | `https://developer.android.com/reference/androidx/paging/LoadStateAdapter#onBindViewHolder(androidx.recyclerview.widget.RecyclerView.ViewHolder,kotlin.Int)(@https://developer.android.com/reference/androidx/annotation/NonNull VH holder, int position)` Called by RecyclerView to display the data at the specified position. |
| `abstract @https://developer.android.com/reference/androidx/annotation/NonNull VH` | `https://developer.android.com/reference/androidx/paging/LoadStateAdapter#onCreateViewHolder(android.view.ViewGroup,androidx.paging.LoadState)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/ViewGroup.html parent, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/LoadState loadState)` Called to create a ViewHolder for the given LoadState. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull VH` | `https://developer.android.com/reference/androidx/paging/LoadStateAdapter#onCreateViewHolder(android.view.ViewGroup,kotlin.Int)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/ViewGroup.html parent, int viewType)` Called when RecyclerView needs a new `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder` of the given type to represent an item. |
| `final void` | `https://developer.android.com/reference/androidx/paging/LoadStateAdapter#setLoadState(androidx.paging.LoadState)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/LoadState loadState)` LoadState to present in the adapter. |
| `void` | `https://developer.android.com/reference/androidx/paging/LoadStateAdapter#setStateRestorationPolicy(androidx.recyclerview.widget.RecyclerView.Adapter.StateRestorationPolicy)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter.StateRestorationPolicy strategy )` |

### Inherited methods

From [androidx.recyclerview.widget.RecyclerView.Adapter](https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter) `final void` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#bindViewHolder(VH,int)(@https://developer.android.com/reference/androidx/annotation/NonNull VH holder, int position)` `final @https://developer.android.com/reference/androidx/annotation/NonNull VH` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#createViewHolder(android.view.ViewGroup,int)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/ViewGroup.html parent, int viewType)` `int` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#findRelativeAdapterPositionIn(androidx.recyclerview.widget.RecyclerView.Adapter<? extends androidx.recyclerview.widget.RecyclerView.ViewHolder>,androidx.recyclerview.widget.RecyclerView.ViewHolder,int)(
@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder> adapter,
@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder viewHolder,
int localPosition
)`

Returns the position of the given `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder` in the given `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter`.
`long` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#getItemId(int)(int position)`

Return the stable ID for the item at `position`.
`final boolean` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#hasObservers()()`

Returns true if one or more observers are attached to this adapter.
`final boolean` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#hasStableIds()()`

Returns true if this adapter publishes a unique `long` value that can act as a key for the item at a given position in the data set.
`final void` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#notifyDataSetChanged()()`

Notify any registered observers that the data set has changed.
`final void` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemChanged(int)(int position)`

Notify any registered observers that the item at `position` has changed.
`final void` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemChanged(int,java.lang.Object)(int position, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/Object.html payload)`

Notify any registered observers that the item at `position` has changed with an optional payload object.
`final void` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemInserted(int)(int position)`

Notify any registered observers that the item reflected at `position` has been newly inserted.
`final void` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemMoved(int,int)(int fromPosition, int toPosition)`

Notify any registered observers that the item reflected at `fromPosition` has been moved to `toPosition`.
`final void` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemRangeChanged(int,int)(int positionStart, int itemCount)`

Notify any registered observers that the `itemCount` items starting at position `positionStart` have changed.
`final void` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemRangeChanged(int,int,java.lang.Object)(
int positionStart,
int itemCount,
@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/Object.html payload
)`

Notify any registered observers that the `itemCount` items starting at position `positionStart` have changed.
`final void` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemRangeInserted(int,int)(int positionStart, int itemCount)`

Notify any registered observers that the currently reflected `itemCount` items starting at `positionStart` have been newly inserted.
`final void` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemRangeRemoved(int,int)(int positionStart, int itemCount)`

Notify any registered observers that the `itemCount` items previously located at `positionStart` have been removed from the data set.
`final void` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemRemoved(int)(int position)`

Notify any registered observers that the item previously located at `position` has been removed from the data set.
`void` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#onAttachedToRecyclerView(androidx.recyclerview.widget.RecyclerView)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView recyclerView)`

Called by RecyclerView when it starts observing this Adapter.
`void` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#onBindViewHolder(VH,int,java.util.List<java.lang.Object>)(
@https://developer.android.com/reference/androidx/annotation/NonNull VH holder,
int position,
@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Object.html> payloads
)` `void` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#onDetachedFromRecyclerView(androidx.recyclerview.widget.RecyclerView)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView recyclerView)`

Called by RecyclerView when it stops observing this Adapter.
`boolean` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#onFailedToRecycleView(VH)(@https://developer.android.com/reference/androidx/annotation/NonNull VH holder)` `void` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#onViewAttachedToWindow(VH)(@https://developer.android.com/reference/androidx/annotation/NonNull VH holder)` `void` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#onViewDetachedFromWindow(VH)(@https://developer.android.com/reference/androidx/annotation/NonNull VH holder)` `void` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#onViewRecycled(VH)(@https://developer.android.com/reference/androidx/annotation/NonNull VH holder)` `void` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#registerAdapterDataObserver(androidx.recyclerview.widget.RecyclerView.AdapterDataObserver)(
@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.AdapterDataObserver observer
)`

Register a new observer to listen for data changes.
`void` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#setHasStableIds(boolean)(boolean hasStableIds)`

Indicates whether each item in the data set can be represented with a unique identifier of type `https://developer.android.com/reference/java/lang/Long.html`.
`void` `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#unregisterAdapterDataObserver(androidx.recyclerview.widget.RecyclerView.AdapterDataObserver)(
@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.AdapterDataObserver observer
)`

Unregister an observer currently listening for data changes.

## Public constructors

### LoadStateAdapter

```
public <VH extends RecyclerView.ViewHolder> LoadStateAdapter()
```

## Public methods

### displayLoadStateAsItem

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

```
public boolean displayLoadStateAsItem(@NonNull LoadState loadState)
```

Returns true if the LoadState should be displayed as a list item when active.

By default, `https://developer.android.com/reference/androidx/paging/LoadState.Loading` and `https://developer.android.com/reference/androidx/paging/LoadState.Error` present as list items, others do not.

### getItemCount

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

```
public final int getItemCount()
```

Returns the total number of items in the data set held by the adapter.

| Returns |
|---|---|
| `int` | The total number of items in this adapter. |

### getItemCount

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

```
public final int getItemCount()
```

### getItemViewType

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

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

Return the view type of the item at `position` for the purposes of view recycling.

The default implementation of this method returns 0, making the assumption of a single view type for the adapter. Unlike ListView adapters, types need not be contiguous. Consider using id resources to uniquely identify item view types.

| Parameters |
|---|---|
| `int position` | position to query |

| Returns |
|---|---|
| `int` | integer value identifying the type of the view needed to represent the item at `position`. Type codes need not be contiguous. |

### getLoadState

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

```
public final @NonNull LoadState getLoadState()
```

LoadState to present in the adapter.

Changing this property will immediately notify the Adapter to change the item it's presenting.

### getStateRestorationPolicy

```
public final @NonNull RecyclerView.Adapter.StateRestorationPolicy getStateRestorationPolicy()
```

### getStateViewType

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

```
public int getStateViewType(@NonNull LoadState loadState)
```

Override this method to use different view types per LoadState.

By default, this LoadStateAdapter only uses a single view type.

### onBindViewHolder

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

```
public abstract void onBindViewHolder(@NonNull VH holder, @NonNull LoadState loadState)
```

Called to bind the passed LoadState to the ViewHolder.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull VH holder` | the ViewHolder to bind to |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/LoadState loadState` | LoadState to display. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/paging/LoadStateAdapter#getItemViewType(kotlin.Int)` |   |
| `https://developer.android.com/reference/androidx/paging/LoadStateAdapter#displayLoadStateAsItem(androidx.paging.LoadState)` |   |

### onBindViewHolder

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

```
public final void onBindViewHolder(@NonNull VH holder, int position)
```

Called by RecyclerView to display the data at the specified position. This method should update the contents of the `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#itemView()` to reflect the item at the given position.

Note that unlike `https://developer.android.com/reference/android/widget/ListView.html`, RecyclerView will not call this method again if the position of the item changes in the data set unless the item itself is invalidated or the new position cannot be determined. For this reason, you should only use the `position` parameter while acquiring the related data item inside this method and should not keep a copy of it. If you need the position of an item later on (e.g. in a click listener), use `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getBindingAdapterPosition()` which will have the updated adapter position. Override `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#onBindViewHolder(VH,int,java.util.List<java.lang.Object>)` instead if Adapter can handle efficient partial bind.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull VH holder` | The ViewHolder which should be updated to represent the contents of the item at the given position in the data set. |
| `int position` | The position of the item within the adapter's data set. |

### onCreateViewHolder

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

```
public abstract @NonNull VH onCreateViewHolder(@NonNull ViewGroup parent, @NonNull LoadState loadState)
```

Called to create a ViewHolder for the given LoadState.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/ViewGroup.html parent` | The ViewGroup into which the new View will be added after it is bound to an adapter position. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/LoadState loadState` | The LoadState to be initially presented by the new ViewHolder. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/paging/LoadStateAdapter#getItemViewType(kotlin.Int)` |   |
| `https://developer.android.com/reference/androidx/paging/LoadStateAdapter#displayLoadStateAsItem(androidx.paging.LoadState)` |   |

### onCreateViewHolder

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

```
public final @NonNull VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
```

Called when RecyclerView needs a new `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder` of the given type to represent an item.

This new ViewHolder should be constructed with a new View that can represent the items of the given type. You can either create a new View manually or inflate it from an XML layout file.

The new ViewHolder will be used to display items of the adapter using `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#onBindViewHolder(VH,int,java.util.List<java.lang.Object>)`. Since it will be re-used to display different items in the data set, it is a good idea to cache references to sub views of the View to avoid unnecessary `https://developer.android.com/reference/android/view/View.html#findViewById(int)` calls.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/ViewGroup.html parent` | The ViewGroup into which the new View will be added after it is bound to an adapter position. |
| `int viewType` | The view type of the new View. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull VH` | A new ViewHolder that holds a View of the given view type. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#getItemViewType(int)` |   |
| `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#onBindViewHolder(VH,int)` |   |

### setLoadState

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

```
public final void setLoadState(@NonNull LoadState loadState)
```

LoadState to present in the adapter.

Changing this property will immediately notify the Adapter to change the item it's presenting.

### setStateRestorationPolicy

```
public void setStateRestorationPolicy(
    @NonNull RecyclerView.Adapter.StateRestorationPolicy strategy
)
```