I'm working on simple code to detect all the gesture like fling, scroll, etc and was going to implement the interface GestureDetector.OnGestureListener
for overriding its methods but I got to know that same could be done with GestureDetector.SimpleOnGestureListener
. As far as I know the SimpleOnGestureListener
is a class which have implemented OnGestureListener
, OnDoubleTapListener
and OnContextClickListener
interfaces and if I'm wrong correct me.
On Android Developer website page it is written -
If you only want to process a few gestures, you can extend
GestureDetector.SimpleOnGestureListener
instead of implementing theGestureDetector.OnGestureListener
interface.
GestureDetector.SimpleOnGestureListener
provides an implementation for all of theon<TouchEvent>
methods by returning false for all of them. Thus you can override only the methods you care about. For example, the snippet below creates a class that extendsGestureDetector.SimpleOnGestureListener
and overridesonFling()
andonDown()
.
I got few questions here,
1) Why to use GestureDetector.SimpleOnGestureListener
if we can implement GestureDetector.OnGestureListener
and other interfaces for using those methods too?
2) Is GestureDetector.SimpleOnGestureListener
same with no difference? It is made to simplify coding?
From the documentation of GestureDetector.SimpleOnGestureListener
A convenience class to extend when you only want to listen for a subset of all the gestures. This implements all methods in the GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener, and GestureDetector.OnContextClickListener but does nothing and return false for all applicable methods.
If you only want to implement a few of the method (not all of them) SimpleOnGestureListener
has default implementation that do nothing. This prevents your code from being cluttered by multiple methods that do nothing. From a functionality standpoint it does not matter if you use SimpleOnGestureListener
or implement the interfaces directly.
Source Code
public static class SimpleOnGestureListener implements OnGestureListener, OnDoubleTapListener,
OnContextClickListener {
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
public void onLongPress(MotionEvent e) {
}
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
return false;
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return false;
}
public void onShowPress(MotionEvent e) {
}
public boolean onDown(MotionEvent e) {
return false;
}
public boolean onDoubleTap(MotionEvent e) {
return false;
}
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
public boolean onSingleTapConfirmed(MotionEvent e) {
return false;
}
public boolean onContextClick(MotionEvent e) {
return false;
}
}