🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Android自助餐之点击事件传递 源码(含图)[下载](http://download.csdn.net/detail/xmh19936688/9372620) ### 看图分析 ![这里写图片描述](https://box.kancloud.cn/2016-03-02_56d65606d6693.jpg "") 图中的Out是ViewGroup的一个子类,如RelativeLayout等。看完图基本就不用分析了 ### 来点代码 先看layout。MyRelativeLayout只是继承RelativeLayout并重写`dispatchTouchEvent`、`onTouchEvent`、`onInterceptTouchEvent`三个方法。MyView也只是重写了`dispatchTouchEvent`、`onTouchEvent`两个方法。 ~~~ <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".activity.MainActivity"> <com.xmh.toucheventdemo.view.MyRelativeLayout android:id="@+id/out" android:tag="out" android:layout_width="300dp" android:layout_height="400dp" android:layout_centerInParent="true" android:background="@color/colorAccent" > <com.xmh.toucheventdemo.view.MyView android:id="@+id/view" android:tag="view" android:layout_width="100dp" android:layout_height="100dp" android:layout_centerInParent="true" android:background="#ffffff" /> </com.xmh.toucheventdemo.view.MyRelativeLayout> </RelativeLayout> ~~~ 父控件MyRelativeLayout.java。个人习惯重写View时重写三个构造方法。 ~~~ public class MyRelativeLayout extends RelativeLayout{ public MyRelativeLayout(Context context) { super(context); } public MyRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); } public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean dispatchTouchEvent(MotionEvent event) { Log.e("xmh", getTag()+"-dispatch:" + TouchEventUtil.getTouchActionName(event)); return super.dispatchTouchEvent(event); // return true; } @Override public boolean onTouchEvent(MotionEvent event) { Log.e("xmh", getTag()+"-touch:" + TouchEventUtil.getTouchActionName(event)); return super.onTouchEvent(event); } @Override public boolean onInterceptTouchEvent(MotionEvent event) { Log.e("xmh", getTag()+"-intercept:" + TouchEventUtil.getTouchActionName(event)); return super.onInterceptTouchEvent(event); // return true; } } ~~~ 子控件MyView.java。MyView跟MyRelativeLayout大同小异,但还是把代码贴上吧。 ~~~ public class MyView extends View{ public MyView(Context context) { super(context); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); } public MyView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean dispatchTouchEvent(MotionEvent event) { Log.e("xmh", getTag() + "-dispatch:" + TouchEventUtil.getTouchActionName(event)); return super.dispatchTouchEvent(event); // return true; } @Override public boolean onTouchEvent(MotionEvent event) { Log.e("xmh", getTag() + "-touch:" + TouchEventUtil.getTouchActionName(event)); return super.onTouchEvent(event); // return true; } } ~~~ 上面代码中的TouchEventUtil是方便打Log的,看代码就明白了。 ~~~ public class TouchEventUtil { public static String getTouchActionName(MotionEvent event) { int actionId=event.getAction(); String actionName = "Unknow:id=" + actionId; switch (actionId) { case MotionEvent.ACTION_DOWN: actionName = "ACTION_DOWN"; break; case MotionEvent.ACTION_MOVE: actionName = "ACTION_MOVE"; break; case MotionEvent.ACTION_UP: actionName = "ACTION_UP"; break; case MotionEvent.ACTION_CANCEL: actionName = "ACTION_CANCEL"; break; case MotionEvent.ACTION_OUTSIDE: actionName = "ACTION_OUTSIDE"; break; } return actionName; } } ~~~ 最后是MainActivity.java ~~~ public class MainActivity extends AppCompatActivity { @Bind(R.id.out)RelativeLayout rlOut; @Bind(R.id.view)View vUp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); } @Override public boolean dispatchTouchEvent(MotionEvent event) { Log.e("xmh","activity-dispatch:"+ TouchEventUtil.getTouchActionName(event)); return super.dispatchTouchEvent(event); } @Override public boolean onTouchEvent(MotionEvent event) { Log.e("xmh","activity-touch:"+ TouchEventUtil.getTouchActionName(event)); return super.onTouchEvent(event); } } ~~~