http://blog.csdn.net/yanzi1225627/article/details/22592831
看过这篇微博之后,好像他贴的ViewGroup那部分日志结果有点问题,为了确定结果如何,决定自己验证一下,也方便以后自己再查看,下面贴上验证的代码。
郭霖的两篇讲解的超级清楚的Android事件分发机制,更加深刻地理解了
http://blog.csdn.net/guolin_blog/article/details/9097463
http://blog.csdn.net/guolin_blog/article/details/9153747
一、代码
1.MainActivity.java
public class MainActivity extends AppCompatActivity { @Bind(R.id.testLinearLayout) TestLinearLayout testLinearLayout; @Bind(R.id.testButton) TestButton testButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); testLinearLayout.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: Log.d(Util.captain, "TestLinearLayout-onTouch-ACTION_DOWN"); break; case MotionEvent.ACTION_UP: Log.d(Util.captain, "TestLinearLayout-onTouch-ACTION_UP"); break; default: break; } return false; } }); testLinearLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(Util.captain, "TestLinearLayout-onClick"); } }); testButton.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: Log.d(Util.captain, "TestButton-onTouch-ACTION_DOWN"); break; case MotionEvent.ACTION_UP: Log.d(Util.captain, "TestButton-onTouch-ACTION_UP"); break; default: break; } return false; } }); testButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(Util.captain, "TestButton-onClick"); } }); } @Override public boolean dispatchTouchEvent(MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: Log.d(Util.captain, "MainActivity-dispatchTouchEvent-ACTION_DOWN"); break; case MotionEvent.ACTION_UP: Log.d(Util.captain, "MainActivity-dispatchTouchEvent-ACTION_UP"); break; default: break; } return super.dispatchTouchEvent(event); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: Log.d(Util.captain, "MainActivity-onTouchEvent-ACTION_DOWN"); break; case MotionEvent.ACTION_UP: Log.d(Util.captain, "MainActivity-onTouchEvent-ACTION_UP"); break; default: break; } return super.onTouchEvent(event); } }
2.MainActivity.xml
<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="com.edward.touchtest.MainActivity"> <com.edward.touchtest.TestLinearLayout android:id="@+id/testLinearLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <com.edward.touchtest.TestButton android:id="@+id/testButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TestButton" /> </com.edward.touchtest.TestLinearLayout> </RelativeLayout>
3.TestLinearLayout.java
public class TestLinearLayout extends LinearLayout { public TestLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override public boolean dispatchTouchEvent(MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: Log.d(Util.captain, "TestLinearLayout-dispatchTouchEvent-ACTION_DOWN"); break; case MotionEvent.ACTION_UP: Log.d(Util.captain, "TestLinearLayout-dispatchTouchEvent-ACTION_UP"); break; default: break; } return super.dispatchTouchEvent(event); } @Override public boolean onInterceptTouchEvent(MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: Log.d(Util.captain, "TestLinearLayout-onInterceptTouchEvent-ACTION_DOWN"); break; case MotionEvent.ACTION_UP: Log.d(Util.captain, "TestLinearLayout-onInterceptTouchEvent-ACTION_UP"); break; default: break; } return super.onInterceptTouchEvent(event); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: Log.d(Util.captain, "TestLinearLayout-onTouchEvent-ACTION_DOWN"); break; case MotionEvent.ACTION_UP: Log.d(Util.captain, "TestLinearLayout-onTouchEvent-ACTION_UP"); break; default: break; } return super.onTouchEvent(event); } }
4.TestButton.java
public class TestButton extends Button { public TestButton(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override public boolean dispatchTouchEvent(MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: Log.d(Util.captain, "TestButton-dispatchTouchEvent-ACTION_DOWN"); break; case MotionEvent.ACTION_UP: Log.d(Util.captain, "TestButton-dispatchTouchEvent-ACTION_UP"); break; default: break; } return super.dispatchTouchEvent(event); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: Log.d(Util.captain, "TestButton-onTouchEvent-ACTION_DOWN"); break; case MotionEvent.ACTION_UP: Log.d(Util.captain, "TestButton-onTouchEvent-ACTION_UP"); break; default: break; } return super.onTouchEvent(event); } }
二、日志结果
1、一次点击事件
01-06 15:17:05.297 30986-30986/com.edward.touchtest D/captain: MainActivity-dispatchTouchEvent-ACTION_DOWN
01-06 15:17:05.297 30986-30986/com.edward.touchtest D/captain: TestLinearLayout-dispatchTouchEvent-ACTION_DOWN
01-06 15:17:05.297 30986-30986/com.edward.touchtest D/captain: TestLinearLayout-onInterceptTouchEvent-ACTION_DOWN
01-06 15:17:05.297 30986-30986/com.edward.touchtest D/captain: TestButton-dispatchTouchEvent-ACTION_DOWN
01-06 15:17:05.297 30986-30986/com.edward.touchtest D/captain: TestButton-onTouch-ACTION_DOWN
01-06 15:17:05.297 30986-30986/com.edward.touchtest D/captain: TestButton-onTouchEvent-ACTION_DOWN
01-06 15:17:06.288 30986-30986/com.edward.touchtest D/captain: MainActivity-dispatchTouchEvent-ACTION_UP
01-06 15:17:06.288 30986-30986/com.edward.touchtest D/captain: TestLinearLayout-dispatchTouchEvent-ACTION_UP
01-06 15:17:06.288 30986-30986/com.edward.touchtest D/captain: TestLinearLayout-onInterceptTouchEvent-ACTION_UP
01-06 15:17:06.288 30986-30986/com.edward.touchtest D/captain: TestButton-dispatchTouchEvent-ACTION_UP
01-06 15:17:06.288 30986-30986/com.edward.touchtest D/captain: TestButton-onTouch-ACTION_UP
01-06 15:17:06.298 30986-30986/com.edward.touchtest D/captain: TestButton-onTouchEvent-ACTION_UP
01-06 15:17:06.298 30986-30986/com.edward.touchtest D/captain: TestButton-onClick
2、注释掉button的OnTouchListener和OnClickListener,再点击一次
01-06 15:43:34.327 10158-10158/com.edward.touchtest D/captain: MainActivity-dispatchTouchEvent-ACTION_DOWN
01-06 15:43:34.327 10158-10158/com.edward.touchtest D/captain: TestLinearLayout-dispatchTouchEvent-ACTION_DOWN
01-06 15:43:34.327 10158-10158/com.edward.touchtest D/captain: TestLinearLayout-onInterceptTouchEvent-ACTION_DOWN
01-06 15:43:34.327 10158-10158/com.edward.touchtest D/captain: TestButton-dispatchTouchEvent-ACTION_DOWN
01-06 15:43:34.327 10158-10158/com.edward.touchtest D/captain: TestButton-onTouchEvent-ACTION_DOWN
01-06 15:43:34.447 10158-10158/com.edward.touchtest D/captain: MainActivity-dispatchTouchEvent-ACTION_UP
01-06 15:43:34.447 10158-10158/com.edward.touchtest D/captain: TestLinearLayout-dispatchTouchEvent-ACTION_UP
01-06 15:43:34.447 10158-10158/com.edward.touchtest D/captain: TestLinearLayout-onInterceptTouchEvent-ACTION_UP
01-06 15:43:34.447 10158-10158/com.edward.touchtest D/captain: TestButton-dispatchTouchEvent-ACTION_UP
01-06 15:43:34.447 10158-10158/com.edward.touchtest D/captain: TestButton-onTouchEvent-ACTION_UP
3、将TestLinearlayout的onInterceptTouchEvent 改成return true,不让孩子知道。
01-06 15:53:04.994 4918-4918/com.edward.touchtest D/captain: MainActivity-dispatchTouchEvent-ACTION_DOWN
01-06 15:53:04.994 4918-4918/com.edward.touchtest D/captain: TestLinearLayout-dispatchTouchEvent-ACTION_DOWN
01-06 15:53:04.994 4918-4918/com.edward.touchtest D/captain: TestLinearLayout-onInterceptTouchEvent-ACTION_DOWN
01-06 15:53:04.994 4918-4918/com.edward.touchtest D/captain: TestLinearLayout-onTouch-ACTION_DOWN
01-06 15:53:04.994 4918-4918/com.edward.touchtest D/captain: TestLinearLayout-onTouchEvent-ACTION_DOWN
01-06 15:53:05.064 4918-4918/com.edward.touchtest D/captain: MainActivity-dispatchTouchEvent-ACTION_UP
01-06 15:53:05.064 4918-4918/com.edward.touchtest D/captain: TestLinearLayout-dispatchTouchEvent-ACTION_UP
01-06 15:53:05.074 4918-4918/com.edward.touchtest D/captain: TestLinearLayout-onTouch-ACTION_UP
01-06 15:53:05.074 4918-4918/com.edward.touchtest D/captain: TestLinearLayout-onTouchEvent-ACTION_UP
01-06 15:53:05.074 4918-4918/com.edward.touchtest D/captain: TestLinearLayout-onClick