Skip to content

Instantly share code, notes, and snippets.

@CDRussell
Created March 21, 2018 01:26
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 CDRussell/e9b9b12048b52bbf2e3f99de5973f4ab to your computer and use it in GitHub Desktop.
Save CDRussell/e9b9b12048b52bbf2e3f99de5973f4ab to your computer and use it in GitHub Desktop.
class TaskListAdapter : RecyclerView.Adapter<ViewHolder>() {
private val tasks: MutableList<Task> = ArrayList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return ViewHolder(inflater.inflate(R.layout.item_task_row, parent, false))
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(tasks[position])
}
override fun getItemCount(): Int = tasks.size
fun addTask(task: Task) {
if (!tasks.contains(task)) {
tasks.add(task)
notifyItemInserted(tasks.size)
}
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(task: Task) {
itemView.taskTitle.text = task.title
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment