我正在尝试在Android应用中学习手势检测。但显然,在使用onFling方法时,我在logcat中什么也得不到。 我在主类上实现了GestureDetector.OnGestureListener。 我从这里看到了手势检测的例子: https://developer.android.com/training/gestures/detector.html#detect
package com.persi.fragmenttester;
import android.content.Intent;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener{
private GestureDetectorCompat mDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDetector=new GestureDetectorCompat(this,this);
mDetector.setOnDoubleTapListener(this);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.d("App",e.toString());
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@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) {
Log.d("App",e.toString());
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d("App", "onFling: " + e1.toString() + e2.toString());
return true;
}
}
答案 0 :(得分:1)
您需要覆盖onTouchEvent()
@Override
public boolean onTouchEvent(MotionEvent event){
if (this.mDetector.onTouchEvent(event)) {
return true;
}
return super.onTouchEvent(event);
}
来自文档:
为了使您的GestureDetector对象能够接收事件, 你覆盖View或Activity的onTouchEvent()方法,然后传递 沿着所有观察到的事件到探测器实例。