onInterceptTouchEvent只获取ACTION_DOWN

为什么ViewGrouponInterceptTouchEvent只能得到ACTION_DOWN ? 根据文档,只要返回false,它应该接收所有的事件types。 http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent%28android.view.MotionEvent%29 Point#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); } } } 

Solutions Collecting From Web of "onInterceptTouchEvent只获取ACTION_DOWN"

我会回答我自己的问题:onInterceptTouchEvent只调用,如果父母有一个从onTouchEvent返回“true”的子视图。 一旦孩子回复正确,父母现在有机会截获该事件。

在这里输入图像说明

我得到同样的问题。 我曾阅读过许多文章:
onInterceptTouchEvent只获取ACTION_DOWN
onInterceptTouchEvent的ACTION_UP和ACTION_MOVE永远不会被调用
onInterceptTouchEvent,onTouchEvent只能看到ACTION_DOWN
onInterceptTouchEvent永远不会收到action_move

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

所有答案都是一样的。 我尝试了很多次,总是没有得到onInterceptTouchEvent()被调用,如果没有down事件。

我阅读源代码,我猜想有些东西是改变的:

 @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)只在onInterceptTouchEvent(ev)时被调用,这是我们试过和发现的。 所以,我猜是,代码被改变了,但是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); } 

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