How to set onItemClickListener for RecyclerView?

12.5k views Asked by At

I'm trying to implement a recyclerView but how can I set an onItemClickListener for my recyclerView?

There are already some answers on SO but they are briefer than what I just need to get the onItemClickListener for RecyclerView.

What is the easiest way to get the onItemClickListener for RecyclerView?

2

There are 2 answers

3
Vinoth Vino On BEST ANSWER

In Android, RecyclerView doesn’t have any listeners like in ListView. So you need to create your own class by extending the RecyclerView.OnItemTouchListener.

Create a class RecyclerTouchListener.java

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

  private GestureDetector gestureDetector;
  private ClickListener clickListener;

public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
    this.clickListener = clickListener;
    gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {

            return true;

        }

        @Override
        public void onLongPress(MotionEvent e) {

            View child = recyclerView.findChildViewUnder(e.getX(), e.getY());

            if (child != null && clickListener != null) {

                clickListener.onLongClick(child, recyclerView.getChildAdapterPosition(child));

            }
        }
    });
}

@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

    View child = rv.findChildViewUnder(e.getX(), e.getY());

    if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {

        clickListener.onClick(child, rv.getChildAdapterPosition(child));

    }
    return false;
}

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}

public interface ClickListener {

    void onClick(View view, int position);

    void onLongClick(View view, int position);
   }
 }

Then in activity, Override the onClick method.

 recyclerView.addOnItemTouchListener(new RecyclerTouchListener(context, recyclerView, new RecyclerTouchListener.ClickListener() {
        @Override
        public void onClick(View view, int position) {

         //   Toast.makeText(getActivity(), position+ " is selected successfully", Toast.LENGTH_SHORT).show();

                //handle click event

        }

        @Override
        public void onLongClick(View view, int position) {

        }
    }));

By this way, you can handle recyclerview item click event.

0
Amir_P On

define a class named RecyclerItemClickListener with this code

class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
    private OnItemClickListener mListener;

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

    private GestureDetector mGestureDetector;

    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 false;
    }

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

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

then in your activity for use this code

recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(MainActivity.this, new RecyclerItemClickListener.OnItemClickListener() {
    @Override
    public void onItemClick(View view, int position) {
    }
}));