Handle touch event for items inside Recyclerview - android

One way is to update your holder to handle any view clicks internally, e.g.

public class CategoryViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener, View.OnClickListener, CompoundButton.OnSetCheckedListener {
    public CategoryViewHolder(View itemView) {
        super(itemView);
        itemView.setOnCreateContextMenuListener(this);
        itemView.setOnClickListener(this);

        catName = (TextView) itemView.findViewById(R.id.category_name);
        catStatus = (SwitchCompat) itemView.findViewById(R.id.category_switch);
        catStatus.setOnCheckChangedListener(this);
    }

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // deal with catStatus change
    }

    public void onClick(View view) {
        // deal with itemView click
    }
}

  1. Create class RecyclerTouchListener

    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.getChildPosition(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.getChildPosition(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);
      }
    }
    
  2. Use this Listener in Activity

    recyclerview.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerview, new RecyclerTouchListener.ClickListener() {
            @Override
            public void onClick(View view, int position) {
    
               // Write your code here
    
            }
    
            @Override
            public void onLongClick(View view, int position) {
    
            }
        }));