在没有onInterceptTouchEvent的情况下调度ACTION_CANCEL

时间:2014-09-07 04:42:05

标签: android motionevent

我正在扩展View并覆盖onTouchEvent 我想在第二根手指触摸屏幕时触发/发送MotionEvent.ACTION_CANCEL。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以调用或覆盖dispatchTouchEvent(MotionEvent事件)。

您想将ACTION_CANCEL发送给谁?例如,如果您的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);
    }
    ...
}