Skip to content

Instantly share code, notes, and snippets.

@ifucolo
Created April 3, 2020 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ifucolo/d77bd5e03518b09781e07785e6687db7 to your computer and use it in GitHub Desktop.
Save ifucolo/d77bd5e03518b09781e07785e6687db7 to your computer and use it in GitHub Desktop.
class LoadStateAdapter : RecyclerView.Adapter<LoadStateAdapter.LoadStateViewHolder>() {
var loadState: LoadState = LoadState.Done
set(value) {
when (field) {
value -> {
notifyItemChanged(0)
}
is LoadState.Loading -> {
itemsCount = 0
notifyItemRemoved(0)
}
is LoadState.Done -> {
itemsCount = 1
notifyItemInserted(1)
}
}
field = value
}
private var itemsCount: Int = 0
inner class LoadStateViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder(parent.inflate(VIEW_ID)) {
fun bind(loadState: LoadState) = with(itemView) {
progress_bar.visible(
visible = LoadState.Loading == loadState
)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = LoadStateViewHolder(parent)
override fun getItemViewType(position: Int): Int = VIEW_ID
override fun getItemCount(): Int = itemsCount
override fun onBindViewHolder(holder: LoadStateViewHolder, position: Int) {
holder.bind(loadState)
}
companion object {
private const val VIEW_ID =
R.layout.item_load_state
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment