Is there any way to set an onClickListener
on a RecyclerView
?
I have a RecyclerView
with some children in it, and setting an <
This is how I have done in Kotlin style
fun RecyclerView.enableClickListener(){
val gesture = object : GestureDetector.SimpleOnGestureListener(){
override fun onSingleTapConfirmed(e: MotionEvent?): Boolean {
this@enableClickListener.performClick()
return super.onSingleTapConfirmed(e)
}
}
val detector = GestureDetector(this.context, gesture)
this.setOnTouchListener { v, event -> detector.onTouchEvent(event) }
}
And this is how to use it
yourRecyclerView.apply {
enableClickListener()
setOnClickListener {
// Do what you want ...
}
}
Enjoy :)