dispatchTouchEvent vs onInterceptTouchEvent vs onTouchEvent

1. xml

<com.torv.lijian.touchclickdemo.MyRelativeLayout xmlns:android=""xmlns:tools=""android:id="@+id/rl_root"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content" /><com.torv.lijian.touchclickdemo.MyRelativeLayout2android:layout_width="match_parent"android:layout_height="wrap_content"><com.torv.lijian.touchclickdemo.MyButtonandroid:id="@+id/btn_touch"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="Touch" /></com.torv.lijian.touchclickdemo.MyRelativeLayout2></com.torv.lijian.touchclickdemo.MyRelativeLayout>

2. root layout:

public class MyRelativeLayout extends RelativeLayout {private static final String TAG = "MyRelativeLayout";public MyRelativeLayout(Context context) {super(context);Log.d(TAG, TAG);}public MyRelativeLayout(Context context, AttributeSet attrs) {super(context, attrs);}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {switch (ev.getActionMasked()) {case MotionEvent.ACTION_DOWN:Log.d(TAG, "dispatchTouchEvent ACTION_DOWN");break;case MotionEvent.ACTION_MOVE:Log.d(TAG, "dispatchTouchEvent ACTION_MOVE");break;case MotionEvent.ACTION_UP:Log.d(TAG, "dispatchTouchEvent ACTION_UP");break;case MotionEvent.ACTION_CANCEL:Log.d(TAG, "dispatchTouchEvent ACTION_CANCEL");break;}return super.dispatchTouchEvent(ev);}@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {switch (ev.getActionMasked()) {case MotionEvent.ACTION_DOWN:Log.d(TAG, "onInterceptTouchEvent ACTION_DOWN");break;case MotionEvent.ACTION_MOVE:Log.d(TAG, "onInterceptTouchEvent ACTION_MOVE");break;case MotionEvent.ACTION_UP:Log.d(TAG, "onInterceptTouchEvent ACTION_UP");break;case MotionEvent.ACTION_CANCEL:Log.d(TAG, "onInterceptTouchEvent ACTION_CANCEL");break;}return super.onInterceptTouchEvent(ev);}@Overridepublic boolean onTouchEvent(MotionEvent ev) {switch (ev.getActionMasked()) {case MotionEvent.ACTION_DOWN:Log.d(TAG, "onTouchEvent ACTION_DOWN");break;case MotionEvent.ACTION_MOVE:Log.d(TAG, "onTouchEvent ACTION_MOVE");break;case MotionEvent.ACTION_UP:Log.d(TAG, "onTouchEvent ACTION_UP");break;case MotionEvent.ACTION_CANCEL:Log.d(TAG, "onTouchEvent ACTION_CANCEL");break;}return super.onTouchEvent(ev);}}

3. parent layout:

public class MyRelativeLayout2 extends RelativeLayout {private static final String TAG = "MyRelativeLayout2";public MyRelativeLayout2(Context context) {super(context);Log.e(TAG, TAG);}public MyRelativeLayout2(Context context, AttributeSet attrs) {super(context, attrs);}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {switch (ev.getActionMasked()) {case MotionEvent.ACTION_DOWN:Log.e(TAG, "dispatchTouchEvent ACTION_DOWN");break;case MotionEvent.ACTION_MOVE:Log.e(TAG, "dispatchTouchEvent ACTION_MOVE");break;case MotionEvent.ACTION_UP:Log.e(TAG, "dispatchTouchEvent ACTION_UP");break;case MotionEvent.ACTION_CANCEL:Log.e(TAG, "dispatchTouchEvent ACTION_CANCEL");break;}return super.dispatchTouchEvent(ev);}@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {switch (ev.getActionMasked()) {case MotionEvent.ACTION_DOWN:Log.e(TAG, "onInterceptTouchEvent ACTION_DOWN");break;case MotionEvent.ACTION_MOVE:Log.e(TAG, "onInterceptTouchEvent ACTION_MOVE");break;case MotionEvent.ACTION_UP:Log.e(TAG, "onInterceptTouchEvent ACTION_UP");break;case MotionEvent.ACTION_CANCEL:Log.e(TAG, "onInterceptTouchEvent ACTION_CANCEL");break;}return super.onInterceptTouchEvent(ev);}@Overridepublic boolean onTouchEvent(MotionEvent ev) {switch (ev.getActionMasked()) {case MotionEvent.ACTION_DOWN:Log.e(TAG, "onTouchEvent ACTION_DOWN");break;case MotionEvent.ACTION_MOVE:Log.e(TAG, "onTouchEvent ACTION_MOVE");break;case MotionEvent.ACTION_UP:Log.e(TAG, "onTouchEvent ACTION_UP");break;case MotionEvent.ACTION_CANCEL:Log.e(TAG, "onTouchEvent ACTION_CANCEL");break;}return super.onTouchEvent(ev);}}

4. childe layout:

public class MyButton extends Button {private static final String TAG = "MyButton";public MyButton(Context context) {super(context);Log.w(TAG, TAG);}public MyButton(Context context, AttributeSet attrs) {super(context, attrs);}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {switch (ev.getActionMasked()) {case MotionEvent.ACTION_DOWN:Log.w(TAG, "dispatchTouchEvent ACTION_DOWN");break;case MotionEvent.ACTION_MOVE:Log.w(TAG, "dispatchTouchEvent ACTION_MOVE");break;case MotionEvent.ACTION_UP:Log.w(TAG, "dispatchTouchEvent ACTION_UP");break;case MotionEvent.ACTION_CANCEL:Log.w(TAG, "dispatchTouchEvent ACTION_CANCEL");break;}return super.dispatchTouchEvent(ev);}@Overridepublic boolean onTouchEvent(MotionEvent ev) {switch (ev.getActionMasked()) {case MotionEvent.ACTION_DOWN:Log.w(TAG, "onTouchEvent ACTION_DOWN");break;case MotionEvent.ACTION_MOVE:Log.w(TAG, "onTouchEvent ACTION_MOVE");break;case MotionEvent.ACTION_UP:Log.w(TAG, "onTouchEvent ACTION_UP");break;case MotionEvent.ACTION_CANCEL:Log.w(TAG, "onTouchEvent ACTION_CANCEL");break;}return super.onTouchEvent(ev);}}

,世上没有绝望的处境,只有对处境绝望的人。

dispatchTouchEvent vs onInterceptTouchEvent vs onTouchEvent

相关文章:

你感兴趣的文章:

标签云: