Q:

Onclick listeners on views in recycler view row

I have a recycler view made up for rows. Each row has a button and an image. I want to add an onClick listener on each image. How can I do that using android and kotlin? I know how to add an onClick listener on the row but not on the elements of the row? 
Here's my code
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.squareup.picasso.Picasso
import com.sumizeit.sumizeit.*
import kotlinx.android.synthetic.main.activity_home.*

class HomeAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private var context: Context
    private var list: ArrayList<HomeRow>

    constructor(context: Context, list: ArrayList<HomeRow>){
        this.context = context
        this.list = list
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val vHolder = holder as YourViewHolder
        Picasso.get().load(list[position].image1).into(vHolder.txtImage1);
        Picasso.get().load(list[position].image2).into(vHolder.txtImage2);
        Picasso.get().load(list[position].image3).into(vHolder.txtImage3);
        Picasso.get().load(list[position].image4).into(vHolder.txtImage4);
        Picasso.get().load(list[position].image5).into(vHolder.txtImage5);

        vHolder.txtImage1.setOnClickListener {
            //TODO something..
            Intent(context, BookDetailActivity::class.java).apply {
                putExtra("bookId", list[position].id1)
            }
        }

        vHolder.txtImage2.setOnClickListener {
            //TODO something..
            Intent(context, BookDetailActivity::class.java).apply {
                putExtra("bookId", list[position].id2)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.home_row, parent, false)
        return YourViewHolder(view)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    var onItemClick: ((pos: Int, view: View) -> Unit)? = null
    inner class YourViewHolder(itemView: View): RecyclerView.ViewHolder(itemView), View.OnClickListener {
        override fun onClick(v: View) {
            onItemClick?.invoke(adapterPosition, v)
        }
        val txtImage1 = itemView.findViewById<ImageView>(R.id.imageView1)
        val txtImage2 = itemView.findViewById<ImageView>(R.id.imageView2)
        val txtImage3 = itemView.findViewById<ImageView>(R.id.imageView3)
        val txtImage4 = itemView.findViewById<ImageView>(R.id.imageView4)
        val txtImage5 = itemView.findViewById<ImageView>(R.id.imageView5)

        init {
            itemView.setOnClickListener(this)
        }
    }
}

in Activity
       var adp = HomeAdapter(this, listHomeRow);
       rv.adapter = adp

A:

change your code 
        override fun onClick(v: View) {
            onItemClick?.invoke(adapterPosition, v)
        }
        val txtImage1 = itemView.findViewById<ImageView>(R.id.imageView1)
        val txtImage2 = itemView.findViewById<ImageView>(R.id.imageView2)
        val txtImage3 = itemView.findViewById<ImageView>(R.id.imageView3)
        val txtImage4 = itemView.findViewById<ImageView>(R.id.imageView4)
        val txtImage5 = itemView.findViewById<ImageView>(R.id.imageView5)

        init {
            itemView.setOnClickListener(this)
        }
    }

to
inner class YourViewHolder(itemView: View): RecyclerView.ViewHolder(itemView), View.OnClickListener {
        override fun onClick(v: View) {
            onItemClick?.invoke(adapterPosition, v)
        }
        val txtImage1 = itemView.findViewById<ImageView>(R.id.imageView1)
        val txtImage2 = itemView.findViewById<ImageView>(R.id.imageView2)
        val txtImage3 = itemView.findViewById<ImageView>(R.id.imageView3)
        val txtImage4 = itemView.findViewById<ImageView>(R.id.imageView4)
        val txtImage5 = itemView.findViewById<ImageView>(R.id.imageView5)

        init {
            txtImage1.setOnClickListener{
                Intent(context, BookDetailActivity::class.java).apply {
                putExtra("bookId", list[layoutPosition].id1)
                context.startActivity(this)
                }
            }
            txtImage2.setOnClickListener{
                Intent(context, BookDetailActivity::class.java).apply {
                putExtra("bookId", list[layoutPosition].id2)
                context.startActivity(this)
                }
            }
        }
    }

and remove clickListener in your bindViewHolder() method. try this bro.

