如何在Android中将手势检测器添加到视图中 [英] How to add a gesture detector to a view in Android

查看:127
本文介绍了如何在Android中将手势检测器添加到视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力将手势检测器添加到项目的子视图中.我要覆盖父级的onTouchEvent还是子级的onTouchEvent?我是否要创建OnTouchListener并在其中添加手势检测器? 文档显示了如何向活动本身添加手势检测器的示例,但是目前尚不清楚如何将其添加到视图中.如果将视图子类化(例如,此处),则可以使用相同的过程,但是我想添加手势而没有任何子类化.

I was struggling with adding a gesture detector to a subview in my project. Do I override the parent's onTouchEvent or the child's onTouchEvent? Do I make an OnTouchListener and add the gesture detector there? The documentation shows an example for how to add a gesture detector to the activity itself but it is not clear how to add it to a view. The same process could be used if subclassing a view (example here), but I want to add the gesture without subclassing anything.

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

This is the closest other question I could find but it is specific to a fling gesture on an ImageView, not to the general case of any View. Also there is some disagreement in those answers about when to return true or false.

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

To help myself understand how it works, I made a stand alone project. My answer is below.

推荐答案

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

This example shows how to add a gesture detector to a view. The layout is just a single View inside of an Activity. You can use the same method to add a gesture detector to any type of view.

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

We will add the gesture detector to the green View.

基本思想是添加 OnTouchListener 到视图.通常,我们会在此处获取所有原始触摸数据(例如ACTION_DOWNACTION_MOVEACTION_UP等),但是与其自己处理,不如将其转发给手势检测器以对手势进行解释.触摸数据.

The basic idea is to add an OnTouchListener to the view. Normally we would get all the raw touch data here (like ACTION_DOWN, ACTION_MOVE, ACTION_UP, etc.), but instead of handling it ourselves, we will forward it on to a gesture detector to do the interpretation of the touch data.

我们正在使用 SimpleOnGestureListener .关于此手势检测器的好处是,我们只需要覆盖所需的手势即可.在这里的示例中,我包括了很多.您可以删除不需要的内容. (不过,您应该始终在onDown()中返回true.返回true表示我们正在处理事件.返回false会使系统停止提供更多触摸事件.)

We are using a SimpleOnGestureListener. The nice thing about this gesture detector is that we only need to override the gestures that we need. In the example here I included a lot of them. You can remove the ones you don't need. (You should always return true in onDown(), though. Returning true means that we are handling the event. Returning false will make the system stop giving us any more touch events.)

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;
        }
    }
}

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

It is a quick setup to run this project, so I recommend you try it out. Notice how and when the log events occur.

这篇关于如何在Android中将手势检测器添加到视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆