【Android学习】View点击时间的分发机制(三)
来源:互联网 发布:4g手机网络变成e怎么办 编辑:程序博客网 时间:2024/05/16 07:08
我们继续来学习Android View事件的分发机制,通过前面两篇博客的介绍相信大家对Android View事件的分发机制有了很深的理解。那么今天我们继续学习ViewGroup的事件分发机制。
1、前言
ViewGroup是View的一个集合,它是继承自View的子类,各种布局也都继承于ViewGroup。ViewGroup实际上也是一个View,只不过比起View,它多了可以包含子View和定义布局参数的功能。在介绍ViewGroup事件的分发机制之前,我们来先看一下第一节三个方法中 onInterceptTouchEvent这个方法的源码:
/** * 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. * * <p>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: * * <ol> * <li> You will receive the down event here. * <li> 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. * <li> 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(). * <li> 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. * </ol> * * @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. */public boolean onInterceptTouchEvent(MotionEvent ev) { return false;}
看着挺吓人的,其实只有一句非常简单的代码,返回false。
2、实际案例
首先我们来自定义一个布局,命名为MyLayout,继承自LinearLayout,如下所示:
然后,打开主布局文件activity_main.xml,在其中加入我们自定义的布局:
可以看到,我们在MyLayout中添加了两个按钮,接着在MainActivity中为这两个按钮和MyLayout都注册了监听事件:我们在MyLayout的onTouch方法,和Button1、Button2的onClick方法中都打印了一句话。现在运行一下项目,效果图如下所示:
分别点击一下Button1、Button2和空白区域,打印结果如下所示:
你会发现,当点击按钮的时候,MyLayout注册的onTouch方法并不会执行,只有点击空白区域的时候才会执行该方法。你可以先理解成Button的onClick方法将事件消费掉了,因此事件不会再继续向下传递。
如果我们把前面的onInterceptTouchEvent方法让它在MyLayout类中返回true。那么现在再次运行项目,然后分别Button1、Button2和空白区域,打印结果如下所示:我们发现,不管点击哪里,永远都只会触发MyLayout的touch事件了,按钮的点击事件完全被屏蔽掉了!这也就是第一节中提到的这个函数的功能是用来判断是否拦截某个事件。在这里就发挥了作用。
原因是Android事件的传递规则是Activity======>>>>>>>Windows======>>>>>>ViewGroup======>>>>>View.
它的示意图如下所示:
由于Activity和ViewGroup都是有点击事件的,也就是他们都有dispatchTouchEvent,onInterceptTouchEvent,
onTouchEvent。如果ViewGroup对事件进行了拦截,那么View将不会收到该点击事件。如果对不拦截,则View将收到该事件,并消耗掉该事件。Viewgroup将不会接收到任何事件。
我们可以用流程图来表示上述传递。
- 【Android学习】View点击时间的分发机制(三)
- 【Android学习】View点击事件的分发机制(一)
- 【Android学习】View点击事件分发机制(二)
- Android View 点击事件的分发机制
- Android自定义View的事件分发机制(三)
- android View的点击事件分发机制的简单认知
- Android View事件分发机制--点击事件的传递规则
- View的点击事件分发机制
- Android事件分发机制源码剖析(2)—顶层View对点击事件的分发过程
- Android View事件分发机制 三 (Activity)
- 关于Android中View的分发机制的学习总结(View篇)
- 【Android学习笔记】Android中View的事件分发机制
- Android View的事件分发机制(一):View
- android的时间分发机制
- Android 学习笔记之四 View的事件分发机制
- Android学习笔记 3.4View的事件分发机制
- 四、界面编程(三) View的事件分发机制
- Android View事件分发机制学习笔记
- 【NOIP2015模拟11.3】IOIOI卡片占卜
- 关于实现代码并行
- android中自定义radiobutton的背景色选择默认图标
- 线段树总结
- 《战拖有术》读书笔记(II)
- 【Android学习】View点击时间的分发机制(三)
- Linux Expect 脚本学习资源
- 最近点对问题
- 面试题的一些总结(1)
- MATLAB地图工具箱学习总结(一)从地图投影说起
- ARM-中断
- java和c语言一些数组差别
- 欢迎使用CSDN-markdown编辑器
- poj(1724)——ROADS