Google은 흑인 공동체를 위한 인종 간 평등을 진전시키기 위해 노력하고 있습니다. Google에서 어떤 노력을 하고 있는지 확인하세요.

LoadStateAdapter

abstract class LoadStateAdapter<VH : RecyclerView.ViewHolder> : RecyclerView.Adapter<VH>
kotlin.Any
   ↳ 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(
    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 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.

Int

Int
getItemViewType(position: Int)

open Int

Override this method to use different view types per LoadState.

Unit
onBindViewHolder(holder: VH, position: Int)

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

Called to bind the passed LoadState to the ViewHolder.

VH
onCreateViewHolder(parent: ViewGroup, viewType: Int)

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

Called to create a ViewHolder for the given LoadState.

Inherited functions
Properties
LoadState

LoadState to present in the adapter.

Public constructors

<init>

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

Public methods

displayLoadStateAsItem

open fun displayLoadStateAsItem(loadState: LoadState): Boolean

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

fun getItemCount(): Int

getItemViewType

fun getItemViewType(position: Int): Int

getStateViewType

open fun getStateViewType(loadState: LoadState): Int

Override this method to use different view types per LoadState.

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

onBindViewHolder

fun onBindViewHolder(
    holder: VH,
    position: Int
): Unit

onBindViewHolder

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

Called to bind the passed LoadState to the ViewHolder.

Parameters
loadState: LoadState LoadState to display.

onCreateViewHolder

fun onCreateViewHolder(
    parent: ViewGroup,
    viewType: Int
): VH

onCreateViewHolder

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

Called to create a ViewHolder for the given LoadState.

Parameters
parent: ViewGroup The ViewGroup into which the new View will be added after it is bound to an adapter position.
loadState: LoadState The LoadState to be initially presented by the new ViewHolder.

Properties

loadState

var loadState: LoadState

LoadState to present in the adapter.

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