Android ViewGroup onInterceptTouchEvent

public boolean onInterceptTouchEvent (MotionEvent ev)

  Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to

your children, and take ownership of the current gesture at any point.

  Using this function takes some care, as it has a fairly complicated interaction with View.onTouchEvent(MotionEvent), and using it

requires implementing that method as well as this one in the correct way. Events will be received in the following order:

  1)You will receive the down event here.

  2)The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle;

this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture.   Also, by

returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing

must happen in onTouchEvent() like normal.

  3)For as long as you return false from this function, each following event  will be delivered first here and then to the target's

onTouchEvent().

  4)If you return true from here, you will not receive any following events. the target view will receive the same event but with

the action ACTION_CANCEL, and all further events will be delivered to your onTouchEvent() method and no longer appear here.

  1)在 onInterceptTouchEvent()接收到down事件

  2)ViewGroup 或者 ViewGroup里面的 View 都可以处理 down事件

  3)只要onInterceptTouchEvent()返回false, 则剩下的事件都会先发送到onInterceptTouchEvent(),再发送到目标对象的onTouchEvent()

  4)如果onInterceptTouchEvent()返回true, 则剩下的事件不会发送到ViewGroup 的 onInterceptTouchEvent()和目标对象的onTouchEvent()

只会发送给ViewGroup 的 onTouchEvent() 

 Returns:

  • Return true to steal motion events from the children and have them dispatched to this ViewGroup through onTouchEvent().

The current target will receive an ACTION_CANCEL event, and no further messages will be delivered here.

返回true会调用ViewGroup的onTouchEvent()。

一。实验代码

1.ViewGroup

public class GlingLayout extends FrameLayout {

    private static String tag = GlingLayout.class.getSimpleName();
    
    public GlingLayout(Context context){
        super(context);
    }
    
    public GlingLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.e(tag, " --- down");
            break;
        case MotionEvent.ACTION_MOVE:
            Log.e(tag, "  --- move");
            break;
        case MotionEvent.ACTION_UP:
            Log.e(tag, "  --- up");
            break;
        case MotionEvent.ACTION_CANCEL:
            Log.e(tag, "  ---  cancel");
            break;
        default:
            break;
        }
        return false;
    }
    
    
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.e(tag, " --------------------- down");
            break;
        case MotionEvent.ACTION_MOVE:
            Log.e(tag, "  ------------------ move");
            break;
        case MotionEvent.ACTION_UP:
            Log.e(tag, "  -----------------  up");
            break;
        case MotionEvent.ACTION_CANCEL:
            Log.e(tag, "  -----------------  cancel");
            break;
        default:
            break;
        }
        return true;
    }
}

2.View

package com.gl;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Button;

public class GlingButton extends Button {

    private static String tag = GlingButton.class.getSimpleName();
    
    public GlingButton(Context context){
        super(context);
    }
    
    public GlingButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.e(tag, " down  ---------------");
            break;
        case MotionEvent.ACTION_MOVE:
            Log.e(tag, " move  --------------- ");
            break;
        case MotionEvent.ACTION_UP:
            Log.e(tag, "  up  ---------------");
            break;
        case MotionEvent.ACTION_CANCEL:
            Log.e(tag, " cancel  ---------------");
            break;
        default:
            break;
        }
        return true;
    }

}

二.结果

1.onInterceptTouchEvent返回false时

2.1.onInterceptTouchEvent返回true时

三。让ViewGroup不能截断TouchEvent

public void requestDisallowInterceptTouchEvent (boolean disallowIntercept)

Called when a child does not want this parent and its ancestors to intercept touch events with onInterceptTouchEvent(MotionEvent).

This parent should pass this call onto its parents. This parent must obey this request for the duration of the touch (that is, only clear the flag after this parent has received an up or a cancel.

Parameters
原文地址:https://www.cnblogs.com/yuyutianxia/p/3700476.html