I have a RelativeLayout I am putting a TouchListener into using GestureDetector. I have already done and can detect double tapping but how can I add a swipe event in the view also?
private void myTapEvent(){
RlContent.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDoubleTap(MotionEvent e) {
count++;
doTaskHere();
return true;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
});
}
After implementing a swipe event, how can I switch between a.) allowing tapping only and disabling swiping and b.) disabling tapping and allowing swiping only.
In your GestureDetector
listener, add the onFling
method. Additionally, to switch between them, you will want a boolean
variable in your class that can be switched.
private boolean mAllowSwipe = true; // True = swipe, no double-tap; false = double-tap, no swipe
// ...
private void switchGestureMode() {
if (mAllowSwipe)
mAllowSwipe = false;
else
mAllowSwipe = true;
}
// ...
private void myTapEvent(){
// ...
gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDoubleTap(MotionEvent e) {
if (mAllowSwipe) {
return false;
}
count++;
doTaskHere();
return true;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (!mAllowSwipe) {
return false;
}
// Your logic here
}
});
}
// ...
There are some examples of swipe using fling here and here, and there's plenty more if you scour Google a bit.