Created
April 3, 2020 20:23
-
-
Save ifucolo/d77bd5e03518b09781e07785e6687db7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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