简体   繁体   English

在没有onInterceptTouchEvent的情况下调度ACTION_CANCEL

[英]dispatch ACTION_CANCEL without onInterceptTouchEvent

I am extending View and overriding onTouchEvent. 我正在扩展View并覆盖onTouchEvent。
I want to fire/dispatch MotionEvent.ACTION_CANCEL when the second finger touch the screen. 当第二根手指触摸屏幕时,我想触发/调度MotionEvent.ACTION_CANCEL。 Is there a way to do that? 有没有办法做到这一点?

You can either call or override dispatchTouchEvent(MotionEvent event). 您可以调用或重写dispatchTouchEvent(MotionEvent event)。

Who do you want to send ACTION_CANCEL to? 您要将ACTION_CANCEL发送给谁? If your View is a ViewGroup for instance, this would send it to all of its children on the second press: 例如,如果您的View是ViewGroup,则在第二次按下时会将其发送给其所有子级:

class MyGroup extends ViewGroup {
    ...
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        // Is this the second finger down?
        if (event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN) {
            // Cancel all children
            MotionEvent cancelEvent = MotionEvent.obtain(event);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                child.dispatchTouchEvent(cancelEvent);
            }
            cancelEvent.recycle();
            return false;
        }
        // Otherwise just do the normal behavior
        return super.dispatchTouchEvent(event);
    }
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM