一张图搞懂Android触摸事件

来源:互联网 发布:wubi安装ubuntu的缺点 编辑:程序博客网 时间:2024/06/01 20:33

Android的触摸事件的传递大体分为Activity ->ViewGroup ->View,触摸事件主要有

dispatchTouchEvent(MotionEvent event)

官方描述为:

Called to process touch screen events. You can override this to intercept all touch screen events before they are dispatched to the window. Be sure to call this implementation for touch screen events that should be handled normally.

这个方法用来分发触摸事件,在Activity、ViewGroup、View中都存在,它是事件传递的第一个分发点,有以下特点:

  • 在Activity中dispatchTouchEvent不论返回true或者false,触摸事件都会被消费;
  • 在ViewGroup或者View中返回false,事件向上一级传递;

onInterceptTouchEvent(MotionEvent ev)

这个方法只在ViewGroup中出现。

onTouchEvent(MotionEvent event)

这个方法Activity、ViewGroup、View中都存在,使用场景为接收下一级dispatchTouchEvent 方法返回值为false的事件。在View中它的super事件指向Activity的onTouch 方法

OnTouchListener接口中的onTouch(View v, MotionEvent event) 事件;

onClick(View v)

上图:

触摸事件的完整流向如图

0 0