2

I am new on kotlin android. I have created the adapter for recyclerview. But I am not able to perform a click event for each recyclerview item. I need the explanation with the reference code. Kindly help me to do this. Thanks in advance. Here is my code for your reference.

  class CustomAdapter(val readerList: ReaderResponse, mainActivity: 
    MainActivity,val btnlistener: BtnClickListener) : 
    RecyclerView.Adapter<CustomAdapter.ViewHolder>() {


    companion object {
        var mClickListener: BtnClickListener? = null
    }

    override fun onCreateViewHolder(viewgroup: ViewGroup, index: Int): ViewHolder 
    {

        val view=LayoutInflater.from(viewgroup?.context).inflate(R.layout.reader_list,viewgroup,false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return readerList.results.size

    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        mClickListener = btnlistener
        val item = readerList

        val reader:ReaderData = readerList.results[position]
        /*p0?.imageview?.text=reader.readerIcon*/
        holder?.reader_status?.text=reader.readerStatus
        holder?.ward_name?.text=reader.wardName
        holder?.reader_id?.text=reader.readerID
        holder?.reader_name?.text=reader.readerName
        holder?.reader_location?.text=reader.readerLocation


        if (reader.readerStatus.toLowerCase().equals("yes")){
            holder.reader_name.setTextColor(Color.parseColor("#24a314"))
        }else if (reader.readerStatus.toLowerCase().equals("no")){

            holder.reader_name.setTextColor(Color.parseColor("#f4312d"))
            holder.warning.setVisibility(View.VISIBLE)
        }
    }



    class ViewHolder(itemView: View) :RecyclerView.ViewHolder(itemView) {

        val imageview = itemView.findViewById(R.id.imageview) as Button
        val reader_name = itemView.findViewById(R.id.reader_name) as TextView
        val reader_location = itemView.findViewById(R.id.floor_no) as TextView
        val ward_name = itemView.findViewById(R.id.ward_name) as TextView
        val reader_id = itemView.findViewById(R.id.reader_id) as TextView
        val reader_status = itemView.findViewById(R.id.reader_status) as TextView
        val warning=itemView.findViewById(R.id.warning) as Button




      }
      open interface BtnClickListener {
        fun onBtnClick(position: Int)
     }

     }
Harsh Jain
  • 1,372
  • 1
  • 9
  • 17
Mayuri
  • 21
  • 1
  • 5

2 Answers2

7

You could use the following approach. This is taken from this blog by Antonio Leiva

Assuming your data class is ReaderData

class CustomAdapter(val readers: List, val listener: (ReaderData) -> Unit) {

    /* Other methods */

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        /*...*/

        holder.imageview.setOnClickListener { 
            listener(readers[position]) 
        }
    }
}

Now in your Activity or Fragment

recyclerview.adapter = CustomAdapter(readersList) { readerData ->
    Log.i(TAG, "${readerData.readerID} clicked")
}

The idea is you pass a lambda which will be executed when your desired item is clicked.

Yaswant Narayan
  • 1,297
  • 1
  • 15
  • 20
0

You just need to implement BtnClickListener in the corresponding Activity in which this adapter is initialized. Once you have implemented the BtnClickListener it would override the function onBtnClick in the activity. The only thing you need to do in the adapter is to initialize the onClickListener on the element you need and in that method just call imageview.setOnClickListener { mClickListener?.onBtnClick(position) }. It would send the position back in activity and you can perform your specific task there. For example I have implemented the ClickListener in one Activity and printed the log there it works fine. Below is the demo code for it.

class Main2Activity : AppCompatActivity(), CustomAdapter.BtnClickListener {
    override fun onBtnClick(position: Int) {
        Log.d("Position", position.toString())
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)

        recyclerView.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)
        val readerResponseList = ArrayList<YourModelClassName>()
        val adapter = CustomAdapter(readerResponseList,this,this)
        recyclerView.adapter = adapter
    }

Hope it Helps.

Harsh Jain
  • 1,372
  • 1
  • 9
  • 17