好记性不如烂笔头,仅以记之。
实例布局如下,每一个View都设置了点击事件监听:
<?xml version="1.0" encoding="utf-8"?>
<com.kongge.touchtestmodule.view.TouchLinearlayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:hexin="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
hexin:touchName="TouchLinearlayout"
android:id="@+id/TouchLinearlayout"
>
<com.kongge.touchtestmodule.view.TouchTextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
hexin:touchName="TouchItemView1"
android:background="#ff0000"
android:id="@+id/TouchItemView1"
/>
<com.kongge.touchtestmodule.view.TouchRelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
hexin:touchName="TouchRelativeLayout"
android:id="@+id/TouchRelativeLayout"
>
<com.kongge.touchtestmodule.view.TouchTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
hexin:touchName="TouchItemView2"
android:background="#00ff00"
android:id="@+id/TouchItemView2"
/>
<com.kongge.touchtestmodule.view.TouchTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
hexin:touchName="TouchItemView3"
android:background="#0000ff"
android:id="@+id/TouchItemView3"
/>
</com.kongge.touchtestmodule.view.TouchRelativeLayout>
</com.kongge.touchtestmodule.view.TouchLinearlayout>
一:dispatchTouchEvent,onInterceptTouchEvent,onTouchEvent
一个touch事件触发时,最外层的ViewGroup(TouchLinearlayout)会调用dispatchTouchEvent分发事件,若是返回true,则后续的touch事件都只会执行到ViewGroup的dispatchTouchEvent;
TouchLinearlayout:dispatchTouchEvent,getAction()=0
TouchLinearlayout:dispatchTouchEvent,getAction()=2
TouchLinearlayout:dispatchTouchEvent,getAction()=2
TouchLinearlayout:dispatchTouchEvent,getAction()=2
TouchLinearlayout:dispatchTouchEvent,getAction()=1
若是返回false,则代表不处理,后面不会收到任何消息。
TouchLinearlayout:dispatchTouchEvent,getAction()=0
当最外层的ViewGroup(TouchLinearlayout) dispatchTouchEvent 返回 super.dispatchTouchEvent(ev)时,它会接着去调用自己的onInterceptTouchEvent方法(只会调用一次),onInterceptTouchEvent返回true代表拦截消息,这样该ViewGroup的子View不会接收到任何消息;
TouchLinearlayout:dispatchTouchEvent,getAction()=0
TouchLinearlayout:onInterceptTouchEvent,getAction()=0
TouchLinearlayout:onTouchEvent,getAction()=0
TouchLinearlayout:dispatchTouchEvent,getAction()=1
TouchLinearlayout:onTouchEvent,getAction()=1
MainActivity:TouchLinearlayout click
返回false,代表不拦截事件,继续向子View分发。
当一个View(TouchItemView3)的onTouchEvent返回false,则代表不再处理Touch事件,该View的dispatchTouchEvent,onInterceptTouchEvent(ViewGroup才有)都不会再被调用。如果返回true,代表消费了这个点击事件,onTouchEvent不会再被传递,后续的点击事件都只会传递到该View。点击TouchItemView3,完整的响应如下
TouchLinearlayout:dispatchTouchEvent,getAction()=0
TouchLinearlayout:onInterceptTouchEvent,getAction()=0
TouchRelativeLayout:dispatchTouchEvent,getAction()=0
TouchRelativeLayout:onInterceptTouchEvent,getAction()=0
TouchItemView3:dispatchTouchEvent,getAction()=0
TouchItemView3:onTouchEvent,getAction()=0
TouchLinearlayout:dispatchTouchEvent,getAction()=1
TouchLinearlayout:onInterceptTouchEvent,getAction()=1
TouchRelativeLayout:dispatchTouchEvent,getAction()=1
TouchRelativeLayout:onInterceptTouchEvent,getAction()=1
TouchItemView3:dispatchTouchEvent,getAction()=1
TouchItemView3:onTouchEvent,getAction()=1
MainActivity:TouchItemView3 click
小结:
dispatchTouchEvent,onInterceptTouchEvent事件的传递是由父到子,由上到下的。
onTouchEvent的响应是由子到父的。
异常:
1.java.lang.IllegalArgumentException: pointerIndex out of range
java.lang.IllegalArgumentException: pointerIndex out of range
at android.view.MotionEvent.nativeGetAxisValue(Native Method)
at android.view.MotionEvent.getX(MotionEvent.java:1979)
请戳->图片缩放时java.lang.IllegalArgumentException: pointerIndex out of range解决方案
这个讲的是ViewPager的问题,我的工程中没有使用ViewPager,是一个PDFView,但是也出现了这个问题,原因应该是一样的,我是在
at com.xxx.XXXActivity.dispatchTouchEvent(ParentFragmentActivity.java:91)
这里加了一个异常捕获。