Skip to content

Instantly share code, notes, and snippets.

@charlesmuchene
Created February 28, 2020 12:07
Show Gist options
  • Select an option

  • Save charlesmuchene/accac1ff1d284bb1e1212596161b0f2b to your computer and use it in GitHub Desktop.

Select an option

Save charlesmuchene/accac1ff1d284bb1e1212596161b0f2b to your computer and use it in GitHub Desktop.
Toy View Adapter
class ToyViewAdapter : RecyclerView.Adapter<ToyViewHolder>() {
private val toys = mutableListOf<Toy>()
fun addToys(vararg toys: Toy) {
val start = toys.size - 1
this.toys.addAll(toys)
notifyItemRangeInserted(start, toys.size)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ToyViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = FragmentToyBinding.inflate(inflater, parent, false)
return ToyViewHolder(binding.root)
}
override fun getItemCount() = toys.size
override fun onBindViewHolder(holder: ToyViewHolder, position: Int) {
holder.bind(toys[position])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment