我有一个实现 onGestureListener 的活动。如何检测同一活动中的双击事件?是否可以识别活动中的双击事件?
它也不识别长按?有什么帮助吗?
GestureDetector
允许您指定OnDoubleTapListener
以及OnGestureListener
. 您唯一需要做的就是实现OnDoubleTapListener
并覆盖它的onDoubleTap
方法。
你也可以只使用SimpleOnGestureListener
和覆盖你想要的。
final Context context = this;
final GestureDetector.SimpleOnGestureListener listener = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDoubleTap(MotionEvent e) {
Toast.makeText(context, "onDoubleTap", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public void onLongPress(MotionEvent e) {
Toast.makeText(context, "onLongPress", Toast.LENGTH_SHORT).show();
}
};
final GestureDetector detector = new GestureDetector(listener);
detector.setOnDoubleTapListener(listener);
detector.setIsLongpressEnabled(true);
getWindow().getDecorView().setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
return detector.onTouchEvent(event);
}
});
对于任何视图,您都可以这样做
ivPost.setOnTouchListener(new View.OnTouchListener() {
private GestureDetector gestureDetector = new GestureDetector(ActivityName.this, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.d("TEST", "onDoubleTap");
return super.onDoubleTap(e);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.d("TEST", "onSingleTap");
return false;
}
});
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}
});
onDoubleTap函数 使用特定元素,如ImageView id,是 ' selectedImage ' & 运行这个函数名称是 ' selectImage() ' 完整代码:
findViewById(R.id.selectdImage).setOnTouchListener(new View.OnTouchListener() {
private GestureDetector gestureDetector = new GestureDetector(MainActivity.this, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDoubleTap(MotionEvent e) {
Toast.makeText(getApplicationContext(), "onDoubleTap", Toast.LENGTH_SHORT).show();
selectImage(nView);
return super.onDoubleTap(e);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.d("onSingleTapConfirmed", "onSingleTap");
return false;
}
});
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}
});