Created
March 21, 2018 01:26
-
-
Save CDRussell/e9b9b12048b52bbf2e3f99de5973f4ab 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 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