android - 如何将手势检测器添加到 Android 中的 View

标签 android touch-event gesturedetector

我一直在努力将手势检测器添加到我项目的 subview 中。我是否覆盖了 parent 的 onTouchEvent 或 child 的 onTouchEvent?我是否制作一个 OnTouchListener 并在那里添加手势检测器? documentation显示了如何将手势检测器添加到 Activity 本身的示例,但不清楚如何将其添加到 View 。如果子类化 View (例如 here ),可以使用相同的过程,但我想添加手势而不子类化任何东西。

This是我能找到的最接近的其他问题,但它特定于 ImageView 上的 throw 手势,而不是任何 View 的一般情况。这些关于何时返回 truefalse 的答案也存在一些分歧。

为了帮助自己理解它的工作原理,我制作了一个独立的项目。我的答案如下。

最佳答案

此示例展示了如何将手势检测器添加到 View 中。布局只是 Activity 中的单个 View。您可以使用相同的方法将手势检测器添加到任何类型的 View 中。

enter image description here

我们将手势检测器添加到绿色的View中。

MainActivity.java

基本思想是添加一个 OnTouchListener到 View 。通常我们会在这里获取所有的原始触摸数据(如 ACTION_DOWNACTION_MOVEACTION_UP 等),但不是自己处理,我们会将其转发给手势检测器,以解释触摸数据。

我们使用的是 SimpleOnGestureListener .这个手势检测器的好处是我们只需要覆盖我们需要的手势。在这里的例子中,我包括了很多。您可以删除不需要的那些。 (不过,您应该始终在 onDown() 中返回 true。返回 true 意味着我们正在处理事件。返回 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;
        }
    }
}

运行此项目是一种快速设置,因此我建议您尝试一下。注意日志事件发生的方式和时间。

关于android - 如何将手势检测器添加到 Android 中的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45054908/

相关文章:

安卓图像按钮

android - div 内有多个 div 的响应式布局

android - 在 Android 上拦截 Activity 和按钮上的触摸事件

java - 在 Android 中轻扫双击?

android - 像 mx 播放器一样滑动手势

安卓开发: multiple tables or a huge single table whilst designing database?

android - 找不到与给定名称 '@style/Widget.RatingBar.Small' 匹配的资源

android - 在 LinearLayout 上实现 OnTouchListener - Android 开发

javascript - 如何禁用触摸滚动?

java - libgdx 中的 GestureDetector 和 InputListener