我用在我的项目加入了手势检测到子视图中挣扎。难道我重写父的onTouchEvent
或孩子的onTouchEvent
?难道我做的OnTouchListener
并添加姿势检测呢?该documentation显示如何将手势检测器添加到活动本身一个例子,但它是不明确如何将它添加到一个视图。同样的过程,如果可以继承的视图(例如here)可以使用,但我想补充的手势没有子什么。
This是最接近的其他问题,我能找到,但它是特定于扫视姿势上ImageView
,不向任何View
的一般情况。也有是在什么时候返回true
或false
这些问题的答案一些分歧。
为了帮助自己理解它是如何工作的,我做了一个独立的项目。我的回答如下。
该示例示出了如何将手势检测器添加到一个视图。布局只是一个活动的内部单一View
。可以使用同样的方法来手势检测器添加到任何类型的图。
我们将姿态探测器添加到绿色View
。
其基本思想是将OnTouchListener
添加到视图。通常我们会得到这里所有的未经处理的触摸数据(如ACTION_DOWN
,ACTION_MOVE
,ACTION_UP
等),但不是处理它自己,我们会将其转发到一个姿态探测器做触摸数据的解释。
我们使用的是SimpleOnGestureListener
。这个姿势检测的好处是,我们只需要重写我们需要的手势。在这里的例子中,我包括了很多。您可以删除不需要的。 (你应该总是在true
返回onDown()
,不过,回到我们正在处理事件的真正手段。返回false将使系统停止给我们更多的触摸事件。)
public class MainActivity extends AppCompatActivity {
private GestureDetector mDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// this is the view we will add the gesture detector to
View myView = findViewById(R.id.my_view);
// get the gesture detector
mDetector = new GestureDetector(this, new MyGestureListener());
// Add a touch listener to the view
// The touch listener passes all its events on to the gesture detector
myView.setOnTouchListener(touchListener);
}
// This touch listener passes everything on to the gesture detector.
// That saves us the trouble of interpreting the raw touch events
// ourselves.
View.OnTouchListener touchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// pass the events to the gesture detector
// a return value of true means the detector is handling it
// a return value of false means the detector didn't
// recognize the event
return mDetector.onTouchEvent(event);
}
};
// In the SimpleOnGestureListener subclass you should override
// onDown and any other gesture that you want to detect.
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent event) {
Log.d("TAG","onDown: ");
// don't return false here or else none of the other
// gestures will work
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.i("TAG", "onSingleTapConfirmed: ");
return true;
}
@Override
public void onLongPress(MotionEvent e) {
Log.i("TAG", "onLongPress: ");
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.i("TAG", "onDoubleTap: ");
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
Log.i("TAG", "onScroll: ");
return true;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
Log.d("TAG", "onFling: ");
return true;
}
}
}
这是一个快速设置来运行这个项目,所以我建议你尝试一下。注意当日志事件发生方式和时间。
在科特林短版检测双击只为一个观点:
val gestureDetector = GestureDetector(activity, object : GestureDetector.SimpleOnGestureListener() {
override fun onDoubleTap(e: MotionEvent?): Boolean {
Log.d("myApp", "double tap")
return true
}
})
myView.setOnTouchListener { _, event -> gestureDetector.onTouchEvent(event) }
不要忘记,使myView
点击