We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

FireZenk • 9 years ago

There is a problem with your code, if you don't want to send more than one click on onInterceptTouchEvent you need to return: return true; like:

if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
return true;
}
return false;

fenny manohar • 9 years ago

i have gone mad wiht the same issue. Thank you for the clue. It worked like wonder.

Daniel Gomez Rico • 10 years ago

Man will be great if you add some color scheme to the snippets :P

Pratik Butani • 9 years ago

You must have to implement `onRequestDisallowInterceptTouchEvent` also.

veritas • 8 years ago

Arif does not know how to implement it.

Omkar Todkar • 9 years ago
Gamerex • 5 years ago

thanks!

Nisha Lad • 7 years ago

there is one error with your code in below line
View childView = rv.findChildViewUnder(e.getX(), e.getY());
error is "Error:(31, 13) error: unreachable statement"

Thank you man!

Gabriel da Silva • 7 years ago

Thank you man! It's works.

Mannar Mannan • 8 years ago

Super.. Works nicely

Eider Diaz • 8 years ago

you save my life sapandiwakar, thank you a lot

Marco Grasselli • 8 years ago

How can I remove the listener for every items of the recyclerview?

donacho • 8 years ago

Thank you! Simple and work!

Rahul Narang • 8 years ago

Hi,
Thanks for this post,
saves my code and time

hamza dabir • 8 years ago

please tell me where to the recyclerview.addOnItemTouchListener method in code i am very new to android

Arif Wahyu • 8 years ago

get problem, please help..

Error:(15, 8) error: RecyclerItemClickListener is not abstract and does not override abstract method onRequestDisallowInterceptTouchEvent(boolean) in OnItemTouchListener

veritas • 8 years ago

Paste this code below public void onTouchEvent function in the recycleritemclicklistener class

@Override
public void onRequestDisallowInterceptTouchEvent(boolean b) {

}

This is called implementing onRequestDisallowInterceptTouchEvent function which it is asking to implement as mentioned by Pratik Butani

Ganesh Giri • 8 years ago

their is problem in method java.lang.NullPointerException: Attempt to invoke interface method' com.example.helper'ItemClickListener.onClick(android.view.View, int, boolean)' on a null object reference

Aurélien Leboulanger • 8 years ago

thank you very much !

Zeynep Yurt • 9 years ago

It works perfectly for RecyclerView thanks :) But I have have problem with RecyclerView with two custom layout.

lv.addOnItemTouchListener(
new RecyclerItemClickListener(getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
@Override public void onItemClick(View view, int position) {
int id= ????????;
if(id==R.layout.etkinlik_layout)
Toast.makeText(getApplicationContext(),"etkinlik",Toast.LENGTH_LONG).show();
else if(id==R.layout.duyuru_layout)
Toast.makeText(getApplicationContext(),"duyuru",Toast.LENGTH_LONG).show();

}
})
);
What should I write to question marked part to get id of clicked item?

Joris Neber • 9 years ago

Great Explanation! Thank you Sapan! :)

Bibaswann Bandyopadhyay • 9 years ago

I've got a hack! Why don't I give the layout (that I'm going to inflate in recyclerview) an id and set onClickListener() on that layout? It's a simpler way when you only need click event, right? Also are you sure your surname is spelled Diwakar, Not Dibakar?

sapandiwakar • 9 years ago

Yes, but this should be done from inside the adapter when inflating the views. This is the recommended way at the moment. And, yes, I am pretty sure my last name is Diwakar ;)

Chirag Patel • 9 years ago

Great Article...Thanks... but i have a one problem when i click on recyclerView item, if 4 items are visible on my device screen then it will call onItemClick() 4 times...

fenny manohar • 9 years ago

if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
return true;
}
return false;

please include this lines in your onInterceptTouchEvent() in Fragment class. Control will take show one child view at a time

Stan Kotokou • 9 years ago

I have done it ! But the problem is still there.. When i add something to the RecyclerView, after refreshing, the Listener didn't applied the option to the new item.

Stan Kotokou • 9 years ago

First I create the listener in Adapter class, so on generating my card view in RecyclerView, by cliking on it i can have some operations... When i initialized the adapter with my default value, i got the listener set to each cardview.. afeter adding a new item, no listener is setting to it ! "
How can applied to every new item the listener option ?

Gagan Gill • 9 years ago

great code.thx to share

Adiyat Mubarak • 9 years ago

Thanks for blogging, its worked! :D

@rpit • 9 years ago

nice article

Bibin Anjanavelil • 9 years ago

good post, thank you..

ofir0013 • 9 years ago

Thanks!

nilesh • 9 years ago

thanks its very usefull and easy code god bless you.

Vishal Srivastava • 9 years ago

Hii Sir, I am getting response on click only after frequently double click??

Cant figure out the issuue??

Vishal Bhandare • 9 years ago

This one is superb. Could you please tell me How to get clicking sound on clicking it RecyclerView Item?

testSmirk • 9 years ago

great code.thx to share

Ravi • 9 years ago

I have a button in recyclerview and when I press button row click listener also get fired? how to get click exclusively on a particular button

Webserveis • 9 years ago

I have similar code
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;

public interface OnItemClickListener {
public void onItemClick(View view, int position);
}

GestureDetector mGestureDetector;

public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}

@Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
return true;
}
return false;
}

@Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }

@Override
public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}
}

jitendra • 9 years ago

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.addOnItemTouchListener(android.support.v7.widget.RecyclerView$OnItemTouchListener)' on a null object reference

Taimur Hassan • 9 years ago

I have faced the same problem and get rid if it by using a unique id. I mean try to change the ids of recycler view in your XML and make them unique and check it.

Rubin Nellikunnathu • 9 years ago

How will i add individual item click if i have an image , button, checkbox in a Recyclerview item? . please help.

Taimur Hassan • 9 years ago

You have to set onClickListeners in your View Holder class, in your Adapter Activity. like below

public ViewHolder(final View itemView, int ViewType) {
super(itemView);

nameUser = (TextView) itemView.findViewById(R.id.name);
emailUser = (TextView) itemView.findViewById(R.id.email);
emailUser.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(mContext, "The Item Clicked " +, Toast.LENGTH_SHORT).show();
}

});

}

Ravi • 9 years ago

have you got your answer?

Jenison • 9 years ago

bro...If u have the answer please post it....in need of it.

Bleh • 9 years ago

You need to set onClickListener in your custom adapter. For example:

imageView = (ImageView) itemView.findViewById(R.id.imageView);
imageView.setOnClickListener(new View.OnClickListener() {
etc etc :)

Nissan Ahmed • 10 years ago

Nice man!

Shubham Waghe • 10 years ago

Okhay so, everything is working fine till now, I am able to detect the individual recycler view item position and able to toast it on clicked. Now I wan't to move on to a new activity and show the details of the item clicked, how can I do that?
Say, I am displaying Contact names list and onclick I wan't to open a new activity show that contact's details...
At least, how can I toast the contact name again on clicking on that contact item, how are the current variables on clicking available to me?
I am planning to bundle those variables and send them with intent and display there. Hope the question is understandable, please suggest a better approach if its there..

Jhoooony • 9 years ago

hi, can u give me a sample to detect individual recycler view item position and able to toast it on clicked ?

Shubham Waghe • 9 years ago

Override the onItemClicked method from above tutorial, the 'position' is the position you need.
Then I simply toasted it by : Toast.makeText(this,position,Toast.LENGTH_SHORT ).show();
inside this method. There is one comment below regarding this

I also refer to Slidenerd video Lecture series, you can check that out, could be helpful.