LoadStateAdapter

public abstract class LoadStateAdapter<VH extends RecyclerView.ViewHolder>


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(
    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)
}

Summary

Public fields

final @NonNull LoadState

LoadState to present in the adapter.

Public constructors

Public methods

boolean

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

final int
final int
getItemViewType(int position)
int

Override this method to use different view types per LoadState.

final void
onBindViewHolder(@NonNull VH holder, int position)
abstract void
onBindViewHolder(@NonNull VH holder, @NonNull LoadState loadState)

Called to bind the passed LoadState to the ViewHolder.

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

Called to create a ViewHolder for the given LoadState.

Inherited methods

From class RecyclerView.Adapter
final void
bindViewHolder(@NonNull VH p0, int p1)
boolean
final @NonNull VH
int
long
getItemId(int p0)
final @NonNull RecyclerView.Adapter.StateRestorationPolicy
final boolean
final boolean
final void
final void
final void
final void
final void
notifyItemMoved(int p0, int p1)
final void
notifyItemRangeChanged(int p0, int p1)
final void
notifyItemRangeChanged(int p0, int p1, @Nullable Object p2)
final void
notifyItemRangeInserted(int p0, int p1)
final void
notifyItemRangeRemoved(int p0, int p1)
final void
void
void
onBindViewHolder(@NonNull VH p0, int p1, @NonNull List<@NonNull Object> p2)
void
boolean
void
void
void
void
void
setHasStableIds(boolean p0)
void
void

Public fields

loadState

@NonNull
public final @NonNull LoadState loadState

LoadState to present in the adapter.

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

Public constructors

LoadStateAdapter

@NonNull
public final <VH extends RecyclerView.ViewHolder> LoadStateAdapter()

Public methods

displayLoadStateAsItem

@NonNull
public boolean displayLoadStateAsItem(@NonNull LoadState loadState)

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

By default, LoadState.Loading and LoadState.Error present as list items, others do not.

getItemCount

@NonNull
public final int getItemCount()

getItemViewType

@NonNull
public final int getItemViewType(int position)

getStateViewType

@NonNull
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

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

onBindViewHolder

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

Called to bind the passed LoadState to the ViewHolder.

Parameters
@NonNull LoadState loadState

LoadState to display.

onCreateViewHolder

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

onCreateViewHolder

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

Called to create a ViewHolder for the given LoadState.

Parameters
@NonNull ViewGroup parent

The ViewGroup into which the new View will be added after it is bound to an adapter position.

@NonNull LoadState loadState

The LoadState to be initially presented by the new ViewHolder.