RecyclerView onDoubleClick and onClick

4.3k views Asked by At

After reading Jacob's solution at RecyclerView onClick question I tried to extend his solution for the "Double Click" event. When I register the recyclerview to both click and double click listeners, onDoubleClick() is correctly called but onClick() is also called when the user double click, which I want to avoid. I want that if the double click event is detected, no single click event be detected.

Below is the code of my double click listener adapted from Jacob's solution :

 public class RecyclerItemDoubleClickListener implements RecyclerView.OnItemTouchListener {
    private OnItemDoubleClickListener doubleClickListener;

    public interface OnItemDoubleClickListener {
        public void onItemDoubleClick(View view, int position);
    }

    GestureDetector mGestureDetector;

    public RecyclerItemDoubleClickListener(Context context, OnItemDoubleClickListener listener) {
        doubleClickListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override public boolean onDoubleTap(MotionEvent e) {
                return true;
            }
        });
    }

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

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

And I simply register like that :

    recyclerView.addOnItemTouchListener(new RecyclerItemDoubleClickListener(
            this, new RecyclerItemDoubleClickListener.OnItemDoubleClickListener() {
        @Override
        public void onItemDoubleClick(View view, int position) {
            Toast.makeText(ActivityMain.this, "double click " + position, Toast.LENGTH_SHORT).show();
        }
    }));

    recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(
            this, new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Toast.makeText(ActivityMain.this, "single click " + position, Toast.LENGTH_SHORT).show();
        }
    }));

How would you handle this?

PS : I also would like to detect "longClick", "swipe left", "swipe right" on the same recyclerview item independently. But I guess that's maybe too much to code by myself. I will wait for libraries or Google to provide it.

2

There are 2 answers

0
Arvin On

use onSingleTapConfirmed() method of the GestureListener to differentiate between the singleTap and DoubleTap.

http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html#onSingleTapConfirmed%28android.view.MotionEvent%29

To detect swipe left and right use onFling() of GestureListener

inside onFling use the following code

if(motionEvent.getX() - motionEvent.getX() > SWIPE_MIN_DISTANCE ) { onSwipeRightToLeft(); } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE ) { onSwipeLeftToRight(); }

0
zayn On

you can use ItemTouchHelperto add swipe to dismiss and drag & drop support to RecyclerView.