Kotlin - Recycleview ViewHolder OnClick not working

I have an app in Kotlin done by another developer. It sets Onclick listeners in "onBindViewHolder" method of RecycleView Adapter. Somehow after the view scrolls out of the visible area (i.e. user scrolls down) and the user scrolls back to top, the onclick method only gets called after the view is tapped TWICE. Don't understand what's going on, and my Kotlin knowledge is very little. This is how the onclick listener is set:

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    holder.itemView.editButton.onClick {
                Log.d("Holder", "Click")



It is working EXCEPT when the view gets out of screen. Also, onBindViewholder is not called for views just coming back from outside of the screen, which is not standard I believe?

There is no special settings for the recycleview either, just a simple linearlayout and a single view type.

Any ideas?

2 answers

  • answered 2018-07-11 04:24 Shivam Kumar

    Try this code.

    holder.itemView.editButton.setOnClickListener(object : View.OnClickListener {
                override fun onClick(v: View?) {
                   Log.d("Holder", "Click")

  • answered 2018-07-11 04:35 Nirav Bhavsar

    Make sure you bind the value of edittext with ViewHolder class like below

    class ViewHolder (view: View) : RecyclerView.ViewHolder(view) {
        // Holds the button that will add each editButton to
        val editButton = view.editButton

    After try this simple code

        holder?.editButton.setOnClickListener {
            Log.e("editbutton", "onClick")