hamid reza gholami hamid reza gholami - 1 year ago 107
Android Question

how to handle both RecyclerView on item click listener for its rows and the views inside it

hello evry one i have recyclerView wich contains some text view and 2 image view i would like to do some task when user click on each row and another task when user click on each of image views .. but i only can handle one of them at the same time now if i set recylerview.addOnItemTouchListener i cant handle images views onclicks with setting on clicklistener for them in the adapter becase when user click on image view the rows click listener will trigger. below code is my
class for handeling recyler view items

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

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

public void onLongItemClick(View view, int position);

GestureDetector mGestureDetector;

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

public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && mListener != null) {
mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));

@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) { }

public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}

and this is my holder adapter

public MyViewHolder(View view) {
tv_word = (TextView) view.findViewById(R.id.tv_word_show);
tv_english = (TextView) view.findViewById(R.id.tv_english_show);
tv_translation = (TextView) view.findViewById(R.id.tv_tranlation_show);
tv_numbershow = (TextView) view.findViewById(R.id.tv_numershow);
fav = (ImageView) view.findViewById(R.id.iv_fav);
more = (ImageView) view.findViewById(R.id.iv_more);
fav.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(mContext,"show some text for test",Toast.LENGTH_LONG).show();

i appreciate any help for my problem

Answer Source

Create Interface in you adapter class.

 public void setClickListener(ClickListener clickListener) {
            this.clickListener = clickListener;

        public interface ClickListener {
            void ItemClicked(View v, int position);

Declare the object on top like this:

ClickListener clickListener;

holder.view.setOnclickListener(new OnclickListener( 
        public void onClick(View v) {
            if (clickListener != null) {
                clickListener.ItemClicked(v, getAdapterPosition());

Implement this interface in class and intialize the clicklistener where you are setting adapter to recyclerview.


When you will implement this in class you will get following method:

    public void ItemClicked(View v, int position) {


Apply switch case from v.getId() you can handle as many click listeners you need to implement.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download