Android 发展 - GestureDetector.OnGestureListener 或 GestureDetector.SimpleOnGestureListener
Android Development - GestureDetector.OnGestureListener or GestureDetector.SimpleOnGestureListener
我正在编写简单的代码来检测所有手势,如滑动、滚动等,并打算实现接口 GestureDetector.OnGestureListener
以覆盖其方法,但我知道同样可以用GestureDetector.SimpleOnGestureListener
。据我所知,SimpleOnGestureListener
是一个 class,它实现了 OnGestureListener
、OnDoubleTapListener
和 OnContextClickListener
接口,如果我错了,请纠正我。
在 Android 开发者网站页面上写着 -
If you only want to process a few gestures, you can extend
GestureDetector.SimpleOnGestureListener
instead of implementing the
GestureDetector.OnGestureListener
interface.
GestureDetector.SimpleOnGestureListener
provides an implementation
for all of the on<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 extends
GestureDetector.SimpleOnGestureListener
and overrides onFling()
and onDown()
.
我在这里有几个问题,
1) 如果我们可以实现 GestureDetector.OnGestureListener
和使用这些方法的其他接口,为什么还要使用 GestureDetector.SimpleOnGestureListener
?
2) GestureDetector.SimpleOnGestureListener
是一样的没有区别吗?它是为了简化编码?
来自 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.
如果您只想实现其中的几个方法(不是全部)SimpleOnGestureListener
有什么都不做的默认实现。这可以防止您的代码被多种无所事事的方法弄得乱七八糟。从功能的角度来看,使用 SimpleOnGestureListener
还是直接实现接口都没有关系。
源代码
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;
}
}
实现应如下所示:
public class MyOnGestureListener implements
GestureOverlayView.OnGestureListener,
GestureDetector.OnDoubleTapListener,
GestureDetector.OnContextClickListener {
@Override public boolean onDoubleTap(MotionEvent event) {return false;}
@Override public boolean onDoubleTapEvent(MotionEvent event) {return false;}
@Override public boolean onSingleTapConfirmed(MotionEvent event) {return false;}
@Override public boolean onContextClick(MotionEvent event) {return false;}
@Override public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {}
@Override public void onGesture(GestureOverlayView overlay, MotionEvent event) {}
@Override public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {}
@Override public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {}
}
我正在编写简单的代码来检测所有手势,如滑动、滚动等,并打算实现接口 GestureDetector.OnGestureListener
以覆盖其方法,但我知道同样可以用GestureDetector.SimpleOnGestureListener
。据我所知,SimpleOnGestureListener
是一个 class,它实现了 OnGestureListener
、OnDoubleTapListener
和 OnContextClickListener
接口,如果我错了,请纠正我。
在 Android 开发者网站页面上写着 -
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()
.
我在这里有几个问题,
1) 如果我们可以实现 GestureDetector.OnGestureListener
和使用这些方法的其他接口,为什么还要使用 GestureDetector.SimpleOnGestureListener
?
2) GestureDetector.SimpleOnGestureListener
是一样的没有区别吗?它是为了简化编码?
来自 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.
如果您只想实现其中的几个方法(不是全部)SimpleOnGestureListener
有什么都不做的默认实现。这可以防止您的代码被多种无所事事的方法弄得乱七八糟。从功能的角度来看,使用 SimpleOnGestureListener
还是直接实现接口都没有关系。
源代码
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;
}
}
实现应如下所示:
public class MyOnGestureListener implements
GestureOverlayView.OnGestureListener,
GestureDetector.OnDoubleTapListener,
GestureDetector.OnContextClickListener {
@Override public boolean onDoubleTap(MotionEvent event) {return false;}
@Override public boolean onDoubleTapEvent(MotionEvent event) {return false;}
@Override public boolean onSingleTapConfirmed(MotionEvent event) {return false;}
@Override public boolean onContextClick(MotionEvent event) {return false;}
@Override public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {}
@Override public void onGesture(GestureOverlayView overlay, MotionEvent event) {}
@Override public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {}
@Override public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {}
}