美文网首页
ViewGroup的onInterceptTouchEvent(

ViewGroup的onInterceptTouchEvent(

作者: 芽芸 | 来源:发表于2017-04-19 16:51 被阅读0次

    public boolean onInterceptTouchEvent(MotionEvent ev) {

    return false;//继续向下传播,为true的时候就直接发送给当前viewGroup的onTouchEvent()方法处理了且不继续向下传播

    }

    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.可以通过实现这个方法去拦截所有的touch事件。这个可以让viewgroup监视分发给它的子view的事件,可以在任何时候取得当前手势事件的控制权。

    Using this function takes some care, as it has a fairly complicated interaction with {@link View#onTouchEvent(MotionEvent) 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:

    You will receive the down event here. 首先,你会在这里接受到down事件。

    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 (instead of looking for a parent view to handle it). 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.down事件可能会被这个viewGroup的一个子孩子处理掉,或者给自己的onTouchEvent()事件处理了。这意味着你需要实现onTouchEvent()方法然后返回true,然后你就会继续接收到剩下的手势事件(而不是继续向上找一个父View去处理这个事件)。同时,onTouchEvent()方法返回true的时候,viewGroup的onInterceptTouchEvent()方法就不会再接收到陆续的方法事件,所有的touch事件会正常的直接给onTouchEvent()方法。

    For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().如果这个方法返回false.接下来的后续事件都会先传递到这里,然后再传递给处理这个事件的目标view的onTouchEvent。

    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 {@link MotionEvent#ACTION_CANCEL}, and all further events will be delivered to your onTouchEvent() method and no longer appear here.如果你这里返回true,你不会再接收到后续的事件:目标view会接受到相同的事件产生的ACTION_CANCEL。。其他的事件也会直接传递到你的onTouchEvent,而再也不会在onInterceptTouchEvent()方法里捕捉到。

    @param ev The motion event being dispatched down the hierarchy. @return 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()事件处理。当前的目标View会接受到ACTION_CANCEL事件,且不会再接受到后续的事件了。

    相关文章

      网友评论

          本文标题:ViewGroup的onInterceptTouchEvent(

          本文链接:https://www.haomeiwen.com/subject/kmdhzttx.html