菜鸟进阶之Android Touch事件传递(二)

这是touch事件传递系列博客的第二篇,如果想了解touch和click的那些事,请浏览投产事件传递系列的第一篇。

理理思路,我发现touch传递这部分的内容很多,所以每篇博客介绍一个方面比较好。这篇博客主要介绍touch事件传递的现象,一个简单的demo,让大家可以看到touch一步一步传递的过程。下篇博客中在研究源码是怎么实现的。再后面的博客会试图改变这篇文章看到的touch的传递过程,比如viewpager里嵌套listview。

demo

代码A:

public class LayoutView1 extends {private final String TAG = "qingtian";public LayoutView1(Context context, AttributeSet attrs) {super(context, attrs);}public boolean onInterceptTouchEvent(MotionEvent ev) {int action = ev.getAction();switch (action) {case MotionEvent.ACTION_DOWN:Log.d(TAG, "LayoutView1 Intercept DOWN");break;case MotionEvent.ACTION_MOVE:Log.d(TAG, "LayoutView1 Intercept MOVE");break;case MotionEvent.ACTION_UP:Log.d(TAG, "LayoutView1 Intercept UP");break;case MotionEvent.ACTION_CANCEL:Log.d(TAG, "LayoutView1 Intercept CANCEL");break;}return false;}@SuppressLint("ClickableViewAccessibility")public boolean onTouchEvent(MotionEvent ev) {int action = ev.getAction();switch (action) {case MotionEvent.ACTION_DOWN:Log.d(TAG, "LayoutView1 Touch DOWN");break;case MotionEvent.ACTION_MOVE:Log.d(TAG, "LayoutView1 Touch MOVE");break;case MotionEvent.ACTION_UP:Log.d(TAG, "LayoutView1 Touch UP");break;case MotionEvent.ACTION_CANCEL:Log.d(TAG, "LayoutView1 Touch CANCEL");break;}}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {Log.d(TAG, "LayoutView1 dispatch " );boolean r = super.dispatchTouchEvent(ev);Log.d(TAG, "LayoutView1 dispatch 结果 "+r);return r;}}

代码B:

public class LayoutView2 extends LinearLayout {private final String TAG = "qingtian";public LayoutView2(Context context, AttributeSet attrs) {super(context, attrs);}public boolean onInterceptTouchEvent(MotionEvent ev) {int action = ev.getAction();switch (action) {case MotionEvent.ACTION_DOWN:Log.d(TAG, "LayoutView2 Intercept DOWN");break;case MotionEvent.ACTION_MOVE:Log.d(TAG, "LayoutView2 Intercept MOVE");break;case MotionEvent.ACTION_UP:Log.d(TAG, "LayoutView2 Intercept UP");break;case MotionEvent.ACTION_CANCEL:Log.d(TAG, "LayoutView2 Intercept CANCEL");break;}return false;}public boolean onTouchEvent(MotionEvent ev) {int action = ev.getAction();switch (action) {case MotionEvent.ACTION_DOWN:Log.d(TAG, "LayoutView2 Touch DOWN");break;case MotionEvent.ACTION_MOVE:Log.d(TAG, "LayoutView2 Touch MOVE");break;case MotionEvent.ACTION_UP:Log.d(TAG, "LayoutView2 Touch UP");break;case MotionEvent.ACTION_CANCEL:Log.d(TAG, "LayoutView2 Touch CANCEL");break;}return false;}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {Log.d(TAG, "LayoutView2 dispatch ");boolean r = super.dispatchTouchEvent(ev);Log.d(TAG, "LayoutView2 dispatch 结果 "+r);return r;}}代码C:

public class MyTextView extends TextView {private final String TAG = "qingtian";public MyTextView(Context context, AttributeSet attrs) {super(context, attrs);}public boolean onTouchEvent(MotionEvent ev) {int action = ev.getAction();switch (action) {case MotionEvent.ACTION_DOWN:Log.d(TAG, "MyTextView Touch DOWN");break;case MotionEvent.ACTION_MOVE:Log.d(TAG, "MyTextView Touch MOVE");break;case MotionEvent.ACTION_UP:Log.d(TAG, "MyTextView Touch UP");break;case MotionEvent.ACTION_CANCEL:Log.d(TAG, "MyTextView Touch CANCEL");break;}return false;}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {Log.d(TAG, "MyTextView dispatch " );boolean r = super.dispatchTouchEvent(ev);Log.d(TAG, "MyTextView dispatch 结果 "+r);return r;}//public void onClick(View v) {//Log.d(TAG, "onClick");//}////public boolean onLongClick(View v) {//Log.d(TAG, "onLongClick");//return false;//}}布局文件D:

<?xml version="1.0" encoding="utf-8"?><com.example.testontouch.LayoutView1 xmlns:android=""android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><com.example.testontouch.LayoutView2android:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="center"android:orientation="vertical" ><com.example.testontouch.MyTextViewandroid:layout_width="200dp"android:layout_height="150dp"android:background="#FFFFFF"android:text="Hello World"android:textColor="#0000FF"android:textSize="40sp"android:textStyle="bold" /></com.example.testontouch.LayoutView2></com.example.testontouch.LayoutView1>

你可以改变这三个方法的返回值,观察touch都会传递到哪里。

相反,某天也许你会忽然发现,心早已沦陷。

菜鸟进阶之Android Touch事件传递(二)

相关文章:

你感兴趣的文章:

标签云: