I created a class that implements GestureDetector.OnGestureListener. However, I do not know how these methods get called. Is there something like view.setOnTouchListener() for a GestureListener that allows you to receive the events? Thank you in advanced. Here is my class
public class GestureHandler implements GestureDetector.OnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private int direction = -1;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
//Left Swipe
if(event1.getX() - event2.getX() > SWIPE_MIN_DISTANCE &&
Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
direction = 270;
Log.d("Swiped: ", direction + "");
return true;
//Right Swipe
} else if(event2.getX() - event1.getX() > SWIPE_MIN_DISTANCE &&
Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
direction = 90;
Log.d("Swiped: ", direction + "");
return true;
}
//Up Swipe
if(event1.getY() - event2.getY() > SWIPE_MIN_DISTANCE &&
Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
direction = 0;
Log.d("Swiped: ", direction + "");
return true;
//Down Swipe
} else if(event2.getY() - event1.getY() > SWIPE_MIN_DISTANCE &&
Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
direction = 180;
Log.d("Swiped: ", direction + "");
return true;
}
direction = -1;
Log.d("Swiped: ", direction + "");
return false;
}
}