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) } } |