您的位置:首页 > 产品设计 > UI/UE

requestDisallowInterceptTouchEvent(),onInterceptTouchEvent()等事件方法-Android

2018-01-03 18:12 519 查看
滑动冲突内部拦截:requestDisallowInterceptTouchEvent(),

滑动冲突外部拦截:onInterceptTouchEvent();dispatchTouchEvent()

Android - requestDisallowInterceptTouchEvent() 阻止父层的View截获touch事件(事件处理机制)- http://blog.csdn.net/CL18652469346/article/details/53184508
android事件之onInterceptTouchEvent,dispatchTouchEvent,onTouchEvent,requestDisallow- http://blog.csdn.net/kongbaidepao/article/details/47342937
> onInterceptTouchEvent(),onTouchEvent()

只有继承viewgroup的控件才有onInterceptTouchEvent()方法

> requestDisallowInterceptTouchEvent()与dispatchTouch() 

View事件的传递顺序是Activity→Window→View,在Activity中的dispatchTouchEvent方法:

public boolean dispatchTouchEvent(MotionEvent ev) {  

    if (ev.getAction() == MotionEvent.ACTION_DOWN) {  

        onUserInteraction();  

    }  

    if (getWindow().superDispatchTouchEvent(ev)) {  

        return true;  

    }  

    return onTouchEvent(ev);  

}  

Android的事件分发机制已经分析的差不多了,我们来总结一下

1,事件的传递顺序是从Activity→Window→View

2,默认情况下View中的dispatchTouchEvent方法会调用onTouchEvent方法(如果OnTouchListener拦截就不会再调用onTouchEvent方法)

3,View中事件的顺序为dispatchTouchEvent→OnTouchListener→onTouchEvent→OnLongClickListener(在DOWN中触发)→OnClickListener(在UP中触发)

4,在ViewGroup中事件的触发顺序为dispatchTouchEvent→onInterceptTouchEvent→onTouchEvent

5,默认情况下Button,ImageButton等Button的子控件都是会消耗事件的,即onTouchEvent默认返回true,而ImageView,TextView,LinearLayout等一些控件默认是不消耗事件的,即onTouchEvent默认返回为false

6,事件是从上往下传递的,如果其中的一个onInterceptTouchEvent返回了true,则表示事件拦截,此后的MOVE,UP都不会再调用onInterceptTouchEvent方法,然后调用自己的onTouchEvent方法,则它下面的控件都不会再获取触发事件

7,如果在子控件中不让父控件拦截,可以调用父控件的requestDisallowInterceptTouchEvent(boolean disallowIntercept)方法,

8,如果子控件不处理,则会往上抛,交给父控件处理,如果都不处理,默认会抛到Activity的onTouchEvent方法,Activity的onTouchEvent方法默认是消耗控件的,且默认返回为false,DOWM,Move,UP都会执行

9,如果有一个控件处理了事件,则后续的一系列事件(MOVE,UP)也都会执行。

对于底层的View来说,有一种方法可以阻止父层的View截获touch事件,就是调用getParent().requestDisallowInterceptTouchEvent(true);方法。一旦底层View收到touch的action后调用这个方法那么父层View就不会再调用onInterceptTouchEvent了,也无法截获以后的action。

用例子总结一下onInterceptTouchEvent和onTouchEvent的调用顺序:

假设最高层View叫OuterLayout,中间层View叫InnerLayout,最底层View叫MyVIew。调用顺序是这样的(假设各个函数返回的都是false)

OuterLayout.onInterceptTouchEvent->InnerLayout.onInterceptTouchEvent->MyView.onTouchEvent->InnerLayout.onTouchEvent->OuterLayout.onTouchEvent。

requestDisallowInterceptTouchEvent(boolean disallowIntercept),当 子View 不想被 父View 拦截的时候,

就可以调用requestDisallowInterceptTouchEvent(MotionEvent)方法,这样,可以放父View的 onInterceptTouchEvent(MotionEvent)失效

简单解决ScrollView 与 WebView 的冲突,dispatchTouchEvent- https://github.com/2954722256/demo_event
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: