GestureDetector.SimpleOnGestureListener和GestureDetectorCompat不起作用.我的代码有什么问题? [英] GestureDetector.SimpleOnGestureListener and GestureDetectorCompat don't work. What's wrong with my code?

查看:255
本文介绍了GestureDetector.SimpleOnGestureListener和GestureDetectorCompat不起作用.我的代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循检测常见手势指南.我已链接到android-support-v4.jar库以获取GestureDetectorCompat,并且我的代码似乎与指南中的代码完全相同,只是我在自定义视图而非活动中检测手势:

I'm following Detecting common gestures guide. I have linked to android-support-v4.jar library to get GestureDetectorCompat, and my code seems exactly the same as in the guide, except I'm detecting gestures in my custom view rather than in activity:

public class MyGlView extends GLSurfaceView {

    private GestureDetectorCompat m_gestureDetector = null;

    public MyGlView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
    public MyGlView(Context context) {
        super(context);
        init(context);
    }
    private void init(Context context) {
        if (m_gestureDetector == null)
            m_gestureDetector = new GestureDetectorCompat(context, new MyGestureListener());

        setEGLContextClientVersion(2);
        setRenderer(new DrawSurfRenderer());
        setRenderMode(RENDERMODE_CONTINUOUSLY); 
    }

@Override
    public boolean onTouchEvent(MotionEvent event) {

        m_gestureDetector.onTouchEvent(event);      
        return super.onTouchEvent(event);
    }

public class MyGestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
        {
            Log.e("", "OnScroll: deltaX=" + String.valueOf(e2.getX() - e1.getX()) + ", deltaY=" + String.valueOf(e2.getY() - e1.getY()));
            return true;
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e)
        {
            Log.e("", "onSingleTapUp: X=" + String.valueOf(e.getX()) + ", Y=" + String.valueOf(e.getY()));
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e)
        {
            Log.e("", "onLongPress: X=" + String.valueOf(e.getX()) + ", Y=" + String.valueOf(e.getY()));
        }
    }

无论我用触摸屏做什么,我只会得到onLongPress.实际上,当我进行快速点击(快速触摸并释放屏幕)时,我将手指从屏幕上移开后仍然会稍微出现onLongPress(怀疑这是一个较长的点击检测延迟).

No matter what I do with the touchscreen, I'm only getting onLongPress. In fact, when I do fast tap (quickly touching and releasing the screen) I still get onLongPress slightly after I remove my finger from the screen ( suspect that's a long tap detection delay).

有什么收获?

推荐答案

我引用 Android指南有关检测常见手势的信息:

是否使用GestureDetector.OnGestureListener,最佳做法是实现一个返回true的onDown()方法.这是因为所有手势均以onDown()消息开头.如果从onDown()返回false,则默认情况下,如GestureDetector.SimpleOnGestureListener所做的那样,系统会假定您要忽略其余手势,并且永远不会调用GestureDetector.OnGestureListener的其他方法.这有可能在您的应用程序中引起意外问题.唯一应该从onDown()返回false的情况是,如果您确实想忽略整个手势.

Whether or not you use GestureDetector.OnGestureListener, it's best practice to implement an onDown() method that returns true. This is because all gestures begin with an onDown() message. If you return false from onDown(), as GestureDetector.SimpleOnGestureListener does by default, the system assumes that you want to ignore the rest of the gesture, and the other methods of GestureDetector.OnGestureListener never get called. This has the potential to cause unexpected problems in your app. The only time you should return false from onDown() is if you truly want to ignore an entire gesture.

您未实现onDown()方法的事实引起了副作用.

The fact that you did not implement the onDown() method was causing side effects.

这篇关于GestureDetector.SimpleOnGestureListener和GestureDetectorCompat不起作用.我的代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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