0

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;
}

}

Derek Dawson
  • 374
  • 3
  • 12

1 Answers1

0

Very simple solution here. :)

Here is how I did this in my project, working with Google Glass -

I didn't actually extend GestureDetector. I had something like this:

public class EXAMPLE {
    private GestureDetector gestureDetector;
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        gestureDetector = createGestureDetector(this);

    }
    private GestureDetector createGestureDetector(Context context) {
        GestureDetector gestureDetectorTemp = new GestureDetector(context, new GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent motionEvent) {
                return false;
            }

            @Override
            public void onShowPress(MotionEvent motionEvent) {
               return false;
            }

            @Override
            public boolean onSingleTapUp(MotionEvent motionEvent) {
               return false;
            }
            @Override
            public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float distanceX, float distanceY) {
                return false;
            }
            @Override
            public void onLongPress(MotionEvent motionEvent) {
            }
            @Override
            public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) { 
             return false;
            }
        });
        return gestureDetectorTemp;
    }

    @Override
    public boolean onGenericMotionEvent(MotionEvent event) {
        if (gestureDetector != null) {
            return gestureDetector.onTouchEvent(event);
        }
        return false;
    }
}

That last part is very important. On any generic motion event, if the gestureDetector isn't null, you'll send the event through the gestureDetector for processing.

KEEP IN MIND ALSO that you need to understand what the return false; and return true; things mean. If you return false, then that means that the event wasn't consumed. If you return true, then the event is consumed. In other words, if you return true, then nothing else will activate, because the event gets 'eaten up,' but if you return false, this sends the event on to other functions which may do something when an action is taken.

I hope this helps. Good luck :)

Alex K
  • 7,771
  • 9
  • 35
  • 54