如何在Android中向视图添加手势检测器

我正在努力为我的项目中的子视图添加手势检测器.我是否覆盖父母的onTouchEvent或孩子的onTouchEvent?我是否制作OnTouchListener并在那里添加手势检测器? documentation显示了如何向活动本身添加手势检测器的示例,但不清楚如何将其添加到视图中.如果继承视图(例如 here),则可以使用相同的过程,但我想添加手势而不进行子类化.

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

为了帮助自己了解它是如何工作的,我做了一个独立的项目.我的答案如下.

解决方法

此示例显示如何向视图添加手势检测器.布局只是Activity内部的一个View.您可以使用相同的方法将手势检测器添加到任何类型的视图.

我们将手势检测器添加到绿色视图中.

MainActivity.java

基本思想是在视图中添加OnTouchListener.通常我们会在这里获得所有原始触摸数据(如ACTION_DOWN,ACTION_MOVE,ACTION_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性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...