How to make custom Click listener for Recyclerview Android Kotlin

Follow me on Instagram : https://www.instagram.com/_rajjani/

CUSTOME CLICK LISTNER IN ANDROID KOTLIN

Hello Guys, We normally use custom listview instead of recyclerview because we need to implement item click event on the same page where we introduce our data and listview. RecyclerViews are more flexible and memory efficient then ListViews. There is easy way to solve this problem and make our own Item Click Listener for Recyclerview Items.

Unlike List Views, RecyclerView does not have default item click event but we can use interface to make our own item click event. To learn more about interfaces read this article of Tutorialspoint click here.

Check out my Video tutorial for step by step learning. In the video i have already created running dummy application with dummy data and simple Recyclerview. After that you need to create interface file and create parameterized function inside interface class. When you call Recycler Items onclick event inside adapter class you need to bind interface function with event as class parameter and all set. Don’t be confuse I have explained all the things in the video tutorial.

Step 1 : Make Interface Class file and function like I did.

Listener Class file onRvClick.kt

package com.raj.recyclerviewclickevent

interface onRvClick {
    fun OnClick(pos: Int)
}

step 2 : Call interface inside Adapter file.

RvAdapter.kt

package com.raj.recyclerviewclickevent

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class RvAdapter(val context: Context, val cityList: ArrayList<String>, val onRvClick: onRvClick) :
    RecyclerView.Adapter<RvAdapter.ViewHolder>() {
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val title = itemView.findViewById<TextView>(R.id.tvName)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RvAdapter.ViewHolder {
        val v = LayoutInflater.from(context).inflate(
            R.layout.rv_layout, parent, false
        )
        return ViewHolder(v)
    }

    override fun onBindViewHolder(holder: RvAdapter.ViewHolder, position: Int) {
        holder.title.text = cityList[position]

        holder.itemView.setOnClickListener {
            onRvClick.OnClick(position)
        }

    }

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

Step 3 : Call Adapter in Main activity and use it with recyclerview

MainActivity.kt

  val rv = findViewById<RecyclerView>(R.id.rvRecyclerview)

        //data list
        val list = arrayListOf<String>("abc", "efg", "hij", "klm", "xyz")


        //loading recycler view
        rv.apply {
            layoutManager = LinearLayoutManager(this@MainActivity, RecyclerView.VERTICAL, false)
            adapter = RvAdapter(this@MainActivity, list, object  : onRvClick{
                override fun OnClick(pos: Int) {
                   Toast.makeText(this@MainActivity, "item at $pos is ${list[pos]}", Toast.LENGTH_SHORT).show()
//you can implement your code here. when user click item of recyclerview
                }

            })
        }

We are all set. Run your code and enjoy 🙂

Subscribe to my channel for more programming and Code videos.

Thanks for reading, Share this article and with your friends.

Raj Avatar