androidandroid-recyclerviewonitemclicklistener

How to set onItemClickListener for RecyclerView?


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?


Solution

  • 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.