Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Add addOnItemClickListener easily to a RecyclerView using Kotlin
import android.view.View
interface OnItemClickListener {
fun onItemClicked(position: Int, view: View)
fun RecyclerView.addOnItemClickListener(onClickListener: OnItemClickListener) {
this.addOnChildAttachStateChangeListener(object: RecyclerView.OnChildAttachStateChangeListener {
override fun onChildViewDetachedFromWindow(view: View?) {
override fun onChildViewAttachedToWindow(view: View?) {
val holder = getChildViewHolder(view)
onClickListener.onItemClicked(holder.adapterPosition, view)
// Usage:
recyclerView.addOnItemClickListener(object: OnItemClickListener {
override fun onItemClicked(position: Int, view: View) {
// Your logic
Copy link

RonnyKibet1 commented Oct 29, 2018

Just awesome! Thank you

Copy link

samuelhoug commented Nov 9, 2018

Thanks, you saved me

Copy link

kashyapasrc commented Jan 8, 2019

Thanks, Saved my day
But how to identify child view instead on itemView.

Copy link

majedalmoqbeli commented Apr 3, 2019

Thank you .

Copy link

fvmzr commented Jun 22, 2019

Thank you , Save my day

Copy link

varadmondkar commented Jul 24, 2019

This is very helpful. Thank you.

Copy link

Puneet1796 commented Sep 20, 2019

Well, That's pretty straight forward and short. Nice work man, 👍

Copy link

yushman commented Oct 22, 2019

Copy link

kiviabrito commented Jan 7, 2020

Great! I just needed to remove the null exceptions(?) and it works great. Thank you!!

Copy link

OwloneDev commented Aug 10, 2020

Great! I just needed to remove the null exceptions(?) and it works great. Thank you!!

It helped me too! Thanks to the author!!!

Copy link

Real-young-man commented Aug 31, 2020


Copy link

ardiansah21 commented Oct 3, 2020

Thanks man... you save my day

Copy link

nishchaljs commented Nov 12, 2020

Thanks a lot!!!

Copy link

yancorrea1995 commented Apr 1, 2022

Thank you!!

Copy link

ramdaramda commented Jun 11, 2022

perfect, thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment