Kotlin Android Extensions in Adapter
Sometimes is difficult to use kotlin android extensions, the best way to use Kotlin synthetic in Adapter or ViewHolder is like this:
1 2 3 4 | override fun onBindViewHolder(holder: ViewHolder, position: Int) {val item = tasks[position]holder.bind(item, checkTask, deleteTask)} |
No need to write
1 2 | val tvTask = view.findViewById(R.id.tvTask)tvTask.text = task.description |
Just
1 | itemView.task_item_textView.text = task.description |
Or in fun bind you can put
1 | = with(itemView) |
All the fun
1 2 3 4 5 6 7 | fun bind(task: Task, checkTask: (Task) -> Unit, deleteTask:(Task) -> Unit) = with(itemView){ task_item_textView.text = task.description task_item_done_checkBox.isChecked = task.completed task_item_done_checkBox.setOnClickListener{checkTask(task)} setOnClickListener { deleteTask(task) } } |
