我们不难发现一个问题,既然在 GestureDetector 实例化时,已经实例化了一个 SimpleOnGestureListener 了,那么在舍近求远的去使用 OnGestureListener 的话,会多出几个无用的空实现,显然很浪费,所以在一般情况下,乖乖的使用 SimpleOnGestureListener 就好了。
最后由于手势监听的方法有点多,大家一时难以记住,所以我打算把所有方法,在 SimpleOnGestureListener 中重写一遍,方便大家进行查阅、记忆:
private final GestureDetector.SimpleOnGestureListener listener = new GestureDetector.SimpleOnGestureListener(){ @Override public boolean onSingleTapConfirmed(MotionEvent e) { MyToast.makeToast(GestureDetectorActivity.this, "single click!"); return super.onSingleTapConfirmed(e); } @Override public boolean onDoubleTap(MotionEvent e) { MyToast.makeToast(GestureDetectorActivity.this, "double click down!"); return super.onDoubleTap(e); } @Override public boolean onDoubleTapEvent(MotionEvent e) { switch (e.getActionMasked()){ case MotionEvent.ACTION_UP: MyToast.makeToast(GestureDetectorActivity.this, "double click up!"); break; } return super.onDoubleTapEvent(e); } @Override public boolean onDown(MotionEvent e) { MyToast.makeToast(GestureDetectorActivity.this, "onDown"); return super.onDown(e); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { mSpeedX = velocityX; mSpeedY = velocityY; handler.postDelayed(runnable, 30); return super.onFling(e1, e2, velocityX, velocityY); } @Override public void onShowPress(MotionEvent e) { MyToast.makeToast(GestureDetectorActivity.this, "onShowPress");// >150ms 时调用 super.onShowPress(e); } @Override public boolean onSingleTapUp(MotionEvent e) {// 双击第一次抬起触发,第二次不触发 Log.d("onSingleTapUp", "onSingleTapUp");// >150ms 时调用 return super.onSingleTapUp(e); } @Override public void onLongPress(MotionEvent e) { MyToast.makeToast(GestureDetectorActivity.this, "onLongPress"); // 后续工作 super.onLongPress(e); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { MyToast.makeToast(GestureDetectorActivity.this, "onScroll X = " + distanceX + " Y = " + distanceY); return super.onScroll(e1, e2, distanceX, distanceY); } };本篇博客,是我对我学习过程的总结,所以其中难免有疏漏,希望大家能在评论区中指出,万分感谢。
同时,如果大家有任何疑问,也可以在评论区中留言、讨论,这个搓衣板跪不跪,你们说了算!