onInterceptTouchEvent 只得到ACTION_DOWN

2022-09-01 11:34:50

为什么 只进入 ?根据文档,只要返回false,它就应该接收所有事件类型。http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent%28android.view.MotionEvent%29第3点。ViewGroupACTION_DOWNonInterceptTouchEvent

示例代码:

public class MainActivity extends Activity {

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new Container(this));
    }

    private class Container extends LinearLayout {

        public Container(Context context) {
            super(context);
            setBackgroundColor(0xFF0000FF);
        }

        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            Log.i(TAG, "onInterceptTouchEvent");
            int action = ev.getActionMasked();
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                Log.i(TAG, "onInterceptTouchEvent.ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i(TAG, "onInterceptTouchEvent.ACTION_MOVE");
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                Log.i(TAG, "onInterceptTouchEvent.ACTION_UP");
                break;
            }
            return super.onInterceptTouchEvent(ev);
        }
    }
}

答案 1

我将回答我自己的问题:onInterceptTouchEvent只有在父级具有从onTouchEvent返回“true”的子视图时才会被调用。一旦子项返回 true,父项现在就有机会截获该事件。

enter image description here


答案 2

我遇到了同样的问题。我读过很多关于它的文章:
onInterceptTouchEvent只得到ACTION_DOWN
onInterceptTouchEvent的ACTION_UP ACTION_MOVE永远不会被调用
InterceptTouchEvent,onTouchEvent只看到ACTION_DOWN
onInterceptTouchEvent永远不会收到action_move

我也读过android doc:
http://developer.android.com/training/gestures/viewgroup.html
http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)

所有答案都是一样的。我试过很多次,总是不上InterceptTouchEvent () 被调用,如果不是关闭事件。

我阅读了源代码,我猜有些东西被改变了:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (mInputEventConsistencyVerifier != null) {
        mInputEventConsistencyVerifier.onTouchEvent(ev, 1);
    }

    boolean handled = false;
    if (onFilterTouchEventForSecurity(ev)) {
        final int action = ev.getAction();
        final int actionMasked = action & MotionEvent.ACTION_MASK;

        // Handle an initial down.
        if (actionMasked == MotionEvent.ACTION_DOWN) {
            // Throw away all previous state when starting a new touch gesture.
            // The framework may have dropped the up or cancel event for the previous gesture
            // due to an app switch, ANR, or some other state change.
            cancelAndClearTouchTargets(ev);
            resetTouchState();
        }

        // Check for interception.
        final boolean intercepted;
        if (actionMasked == MotionEvent.ACTION_DOWN
                || mFirstTouchTarget != null) {
            final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
            if (!disallowIntercept) {
                intercepted = onInterceptTouchEvent(ev);
                ev.setAction(action); // restore action in case it was changed
            } else {
                intercepted = false;
            }
        } else {
            // There are no touch targets and this action is not an initial down
            // so this view group continues to intercept touches.
            intercepted = true;
        }


根据上面的代码,只有当调用时,这就是我们尝试和发现的。所以,我猜的是,代码被改变了,但文档没有。onInterceptTouchEvent(ev)MotionEvent.ACTION_DOWN

如果要监视或监视所有事件,包括已发送到子视图的事件,则可以按如下方式覆盖:dispatchTouchEvent()

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    MyLog.d(MyLog.DEBUG, "dispatchTouchEvent(): "+event.getAction());
    if (isEnabled()) {
        MyLog.d(MyLog.DEBUG, "dispatchTouchEvent()2: "+event.getAction());

        processEvent(event);//here you get all events include move & up

        super.dispatchTouchEvent(event);

        return true; //to keep receive event that follow down event
    }
    return super.dispatchTouchEvent(event);
}

我有可运行的代码:https://github.com/maxyou/gesturebutton/blob/master/src/com/maxproj/gesturebutton/GestureButtonLayout.java


推荐