onInterceptTouchEvent只获取ACTION_DOWN

use*_*321 26 java android

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

示例代码:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

use*_*321 46

我将回答我自己的问题:如果父项具有从onTouchEvent返回"true"的子视图,则仅调用onInterceptTouchEvent.一旦孩子返回true,父母就有机会拦截该事件.

在此输入图像描述

  • 如果子进程已经调度了该事件,父进程如何拦截该事件? (3认同)
  • 孩子们不会调度触摸事件(通常)。触摸事件迭代地从父级冒泡到子级。往下是onInterceptTouchEvent,往上是onTouchEvent。它会继续冒泡,直到有人返回 true。 (2认同)

hyy*_*010 7

我遇到了同样的问题.我读过很多关于它的帖子:
onInterceptTouchEvent只获取ACTION_DOWN
onInterceptTouchEvent的ACTION_UP而ACTION_MOVE永远不会被调用
onInterceptTouchEvent,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)

所有答案都一样.我尝试了很多次,如果没有down事件,总是不会调用onInterceptTouchEvent().

我看了源代码,我猜有些东西改变了:

@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;
        }
Run Code Online (Sandbox Code Playgroud)


根据上面的代码,onInterceptTouchEvent(ev)只是在调用的时候MotionEvent.ACTION_DOWN,这是我们尝试和发现的.所以,我猜测的是,代码已经改变了,但是doc没有.

如果您想要间谍或监视所有事件包括那些发送到子视图的事件,您可以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);
}
Run Code Online (Sandbox Code Playgroud)

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