从onDown()返回false的含义 [英] The meaning of returning false from onDown()

查看:77
本文介绍了从onDown()返回false的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 android培训,如果您扩展GestureDetector.SimpleOnGestureListener,并且从onDown(...)返回false的方式比从GestureDetector.SimpleOnGestureListener的其他方法永远不会被调用:

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

但是,在我的简单测试中,调用了onScroll(...).

public void onCreate(Bundle savedInstanceState) {
    mDetector = new GestureDetectorCompat(this, MyGestureListener);
}


public boolean onTouchEvent(MotionEvent event) { 
    mDetector.onTouchEvent(event);
    return true;
}


class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
    private boolean scrollEvent;

    @Override
    public boolean onDown (MotionEvent event) {
        Log.v("GESTURE", "onDown ");
        return false;
    }

    @Override
    public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        Log.v("GESTURE", "onScroll");
        return true;
    }

另一个类似的问题是下一个定义,同样来自相同的android培训页面:

各个方法上的true返回值表示您已处理touch事件.返回值为false的事件将事件向下传递到视图堆栈,直到成功处理触摸为止.

如何用上一个报价来解决?

解决方案

您唯一应该从onDown()返回false的情况是您是否真的想忽略整个手势.

这说明了一切.

关键是onDown(...)方法接收到MotionEvent作为参数,您可以选择分析onDown(...)方法中的MotionEvent,如果不是要处理的内容,则返回false.

MotionEvent包含许多详细信息,包括手势开始的位置(例如)-如果它在您要处理的区域之外,则返回false,否则返回true.

如果从onDown(...)返回true,则将调用其他方法.这些方法中的每一个都可以选择分析和处理传递给它们的各种参数.如果您在这些方法中的任何一个方法中处理事件,并且不希望采取任何进一步的操作,则从这些方法中返回true,否则将调用其他方法(可能在超类中,具体取决于您的代码实现).

手势很复杂,涉及上下动作以及向任何方向的移动.允许选项拒绝手势(通过在onDown(...)中返回false)使事情变得更加通用.

编辑:在某些情况下,屏幕上可能会有多个视图.传递给onDown(...)MotionEvent将包含有关手势开始位置的信息.如果您不希望屏幕的某些区域对手势做出反应,则在检查手势的开始位置后会返回false.

According to android training if you extend GestureDetector.SimpleOnGestureListener, and return false from onDown(...) than the other methods of GestureDetector.SimpleOnGestureListener will never get called:

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.

However, in my simple test onScroll(...) is been called.

public void onCreate(Bundle savedInstanceState) {
    mDetector = new GestureDetectorCompat(this, MyGestureListener);
}


public boolean onTouchEvent(MotionEvent event) { 
    mDetector.onTouchEvent(event);
    return true;
}


class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
    private boolean scrollEvent;

    @Override
    public boolean onDown (MotionEvent event) {
        Log.v("GESTURE", "onDown ");
        return false;
    }

    @Override
    public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        Log.v("GESTURE", "onScroll");
        return true;
    }

Another similar issue is the next definition, again from the same android training page:

a return value of true from the individual on methods indicates that you have handled the touch event. A return value of false passes events down through the view stack until the touch has been successfully handled.

How does this settle with the previous quotation?

解决方案

The only time you should return false from onDown() is if you truly want to ignore an entire gesture.

That pretty much says it all.

The point is the onDown(...) method receives a MotionEvent as a parameter, You have the option to analyse the MotionEvent in the onDown(...) method and if it isn't something you want to handle then you return false.

A MotionEvent carries a lot of detailed information which includes position of the start of the gesture (for example) - if it's outside of an area you want to handle then return false otherwise return true.

If you return true from onDown(...) the other methods will then be called. Each of those methods again have the option to analyse and handle the various parameters passed to them. If you handle an event in any of those methods and don't want any further action then return true from those methods otherwise the other methods will be called (possibly in a super class depending on your code implementation).

Gestures are complex and involve down and up actions as well as movement in any direction. Allowing the option to reject a gesture (by returning false in onDown(...)) makes things more versatile.

EDIT: In some situations there may be a case where you have multiple views on a screen. The MotionEvent passed to onDown(...) will contain information about where the gesture starts. If you don't wan't some areas of your screen to react to gestures then you return false when you've checked the start position of the gesture.

这篇关于从onDown()返回false的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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