Google berkomitmen untuk mendorong terwujudnya keadilan ras bagi komunitas Kulit Hitam. Lihat caranya.


abstract class LoadStateAdapter<VH : RecyclerView.ViewHolder> : RecyclerView.Adapter<VH>
   ↳ androidx.recyclerview.widget.RecyclerView.Adapter<VH>
   ↳ androidx.paging.LoadStateAdapter

Adapter for displaying a RecyclerView item based on LoadState, such as a loading spinner, or a retry error button.

By default will use one shared view type for all items.

By default, both LoadState.Loading and LoadState.Error are presented as adapter items, other states are not. To configure this, override displayLoadStateAsItem.

To present this Adapter as a header and or footer alongside your PagingDataAdapter, see PagingDataAdapter.withLoadStateHeaderAndFooter, or use ConcatAdapter directly to concatenate Adapters.

class LoadStateViewHolder(
    parent: ViewGroup,
    retry: () -> Unit
) : RecyclerView.ViewHolder(
        .inflate(R.layout.load_state_item, parent, false)
) {
    private val progressBar: ProgressBar = itemView.findViewById(
    private val errorMsg: TextView = itemView.findViewById(
    private val retry: Button = itemView.findViewById<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) {
    } else {

 * 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) =


Public constructors

Adapter for displaying a RecyclerView item based on LoadState, such as a loading spinner, or a retry error button.

Public methods
open Boolean

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


getItemViewType(position: Int)

open Int

Override this method to use different view types per LoadState.

onBindViewHolder(holder: VH, position: Int)

abstract Unit
onBindViewHolder(holder: VH, loadState: LoadState)

Called to bind the passed LoadState to the ViewHolder.

onCreateViewHolder(parent: ViewGroup, viewType: Int)

abstract VH
onCreateViewHolder(parent: ViewGroup, loadState: LoadState)

Called to create a ViewHolder for the given LoadState.

Inherited functions