Created
February 28, 2020 12:07
-
-
Save charlesmuchene/accac1ff1d284bb1e1212596161b0f2b to your computer and use it in GitHub Desktop.
Toy View Adapter
This file contains hidden or 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 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