1

I have started to code in kotlin newly. my problem is i can't assign an item click listener to my recyclerview. i have spend to days for this and steel nothing. i tried to use this approach : https://gist.github.com/arcadefire/1e3a95314fdbdd78fb211b099d6ec9da i add the code but nothing happens when clicking on recyclerview item. any help will be appertained this is this is code i used and didn't work:

    interface OnItemClickListener {
    fun onItemClicked(position: Int, view: View)
}

fun RecyclerView.addOnItemClickListener(onClickListener: OnItemClickListener) {
    this.addOnChildAttachStateChangeListener(object: RecyclerView.OnChildAttachStateChangeListener {
        override fun onChildViewDetachedFromWindow(view: View?) {
            view?.setOnClickListener(null)
        }

        override fun onChildViewAttachedToWindow(view: View?) {
            view?.setOnClickListener({
                val holder = getChildViewHolder(view)
                onClickListener.onItemClicked(holder.adapterPosition, view)
            })
        }
    })
}

// Usage:
recyclerView.addOnItemClickListener(object: OnItemClickListener {
    override fun onItemClicked(position: Int, view: View) {
        // Your logic
    }
})
Jimale Abdi
  • 2,574
  • 5
  • 26
  • 33
saber javadi
  • 473
  • 1
  • 7
  • 17
  • Please post your code, not the code of the example gist, so we can see where the problem is – gpunto Nov 02 '19 at 08:56
  • 1
    Possible duplicate of [RecyclerView itemClickListener in Kotlin](https://stackoverflow.com/questions/29424944/recyclerview-itemclicklistener-in-kotlin) – Jakir Hossain Nov 02 '19 at 09:04

0 Answers0