0

Hi I'm using swipe layout in my recyclerview. Also I want to implement single and double tap for my list item layout. If i use touch listener my swipe will not work. So i want to detect single and double click of an layout using setOnClickListener. Please suggest me an idea to detect single and double tap for a layout.

Sangeetha
  • 496
  • 1
  • 7
  • 25

3 Answers3

2

Hi I found a solution both single tap and double tap will work for single view using onSingleTapConfirmed like below,

  public class OnSwipeTouchListener implements View.OnTouchListener {

private GestureDetector gestureDetector;

public OnSwipeTouchListener(Context c) {
    gestureDetector = new GestureDetector(c, new GestureListener());
}

public boolean onTouch(final View view, final MotionEvent motionEvent) {
    return gestureDetector.onTouchEvent(motionEvent);
}

private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {

        return super.onSingleTapUp(e);
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        onClick();
        return super.onSingleTapConfirmed(e);
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        onDoubleClick();
        return super.onDoubleTap(e);
    }

    @Override
    public void onLongPress(MotionEvent e) {
        onLongClick();
        super.onLongPress(e);
    }

    // Determines the fling velocity and then fires the appropriate swipe event accordingly
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeDown();
                    } else {
                        onSwipeUp();
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeUp() {
}

public void onSwipeDown() {
}

public void onClick() {

}

public void onDoubleClick() {

}

public void onLongClick() {

}}

then implement this class like below,

    btn.setOnTouchListener(new OnSwipeTouchListener(adapter.getContext()) {

        @Override
        public void onClick() {
            super.onClick();
            AlertUtils.showToast(adapter.getContext(),"single tap");
            // your on click here
        }

        @Override
        public void onDoubleClick() {
            super.onDoubleClick();
            // your on onDoubleClick here
        }

        @Override
        public void onLongClick() {
            super.onLongClick();
            // your on onLongClick here
        }

        @Override
        public void onSwipeUp() {
            super.onSwipeUp();
            // your swipe up here
        }

        @Override
        public void onSwipeDown() {
            super.onSwipeDown();
            // your swipe down here.
        }

        @Override
        public void onSwipeLeft() {
            super.onSwipeLeft();
            // your swipe left here.
        }

        @Override
        public void onSwipeRight() {
            super.onSwipeRight();
            // your swipe right here.
        }
    });

Happy coding..

Sangeetha
  • 496
  • 1
  • 7
  • 25
0

Use the following steps:

  1. Add on touch listener to the view
  2. Add a global variable to record the time of the touch
  3. Compare the touch time with previous touch time. If difference is less than threshold for example 1 seconds, then it is a double tap.
Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57
0

Try this

int c = 0;
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    c++;
    Handler handler = new Handler();
    Runnable r = new Runnable() {

        @Override
        public void run() {
            c = 0;
        }
    };

    if (c == 1) {
        //Single click
        handler.postDelayed(r, 250);
    } else if (c == 2) {
        //Double click
        c = 0;
        ShowDailog();
    }


}
});
Dalvinder Singh
  • 1,073
  • 1
  • 12
  • 19
  • Hi, Thanks for you response. When double tapping the button control first goes to single click part and then comes to double click part. But i'm double tapping. – Sangeetha Aug 12 '16 at 08:42
  • both single and double tap for a button is not working using your code. – Sangeetha Aug 12 '16 at 10:17