- 蓓蓓
-
dispatchtouchevent 什么时候被调用
1.dispatchTouchEvent的执行顺序为:
首先触发ACTIVITY的dispatchTouchEvent
然后触发ACTIVITY的onUserInteraction
然后触发LAYOUT的dispatchTouchEvent
然后触发LAYOUT的onInterceptTouchEvent
这就解释了重写ViewGroup时必须调用super.dispatchTouchEvent();
2.(1)dispatchTouchEvent:
此方法一般用于初步处理事件,因为动作是由此分发,所以通常会调用
super.dispatchTouchEvent。这样就会继续调用onInterceptTouchEvent,再由onInterceptTouchEvent决定事件流向。
(2)onInterceptTouchEvent:
若返回值为True事件会传递到自己的onTouchEvent();
若返回值为False传递到下一个view的dispatchTouchEvent();
(3)onTouchEvent():
若返回值为True,事件由自己处理消耗,后续动作序列让其处理;
若返回值为False,自己不消耗事件了,向上返回让其他的父view的onTouchEvent接受处理;
- 豆豆staR
-
* 回复内容中包含的链接未经审核,可能存在风险,暂不予完整展示!新建一个类MyButton,该类继承自Button
然后重写其中的onTouchEvent和dispatchTouchEvent方法
public class MyButton extends Button { private Context mContext; public MyButton(Context context) { super(context); this.mContext = context; } public MyButton(Context context, AttributeSet attrs) { super(context, attrs); this.mContext = context; } @Override public boolean dispatchTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.d("TTTT", "context:" + mContext + "MyButton|dispatchTouchEvent|return:" + super.dispatchTouchEvent(event) + "|event:DOWN"); break; case MotionEvent.ACTION_MOVE: Log.d("TTTT", "context:" + mContext + "MyButton|dispatchTouchEvent|return:" + super.dispatchTouchEvent(event) + "|event:MOVE"); break; case MotionEvent.ACTION_UP: Log.d("TTTT", "context:" + mContext + "MyButton|dispatchTouchEvent|return:" + super.dispatchTouchEvent(event) + "|event:UP"); break; } Log.d("TTTT", "========================== "); return super.dispatchTouchEvent(event); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.d("TTTT", "context:" + mContext + "MyButton|onTouchEvent|return:" + super.onTouchEvent(event) + "|event:DOWN"); break; case MotionEvent.ACTION_MOVE: Log.d("TTTT", "context:" + mContext + "MyButton|onTouchEvent|return:" + super.onTouchEvent(event) + "|event:MOVE"); break; case MotionEvent.ACTION_UP: Log.d("TTTT", "context:" + mContext + "MyButton|onTouchEvent|return:" + super.onTouchEvent(event) + "|event:UP"); break; } return super.onTouchEvent(event); } }
然后将这个Button加入到布局文件中,然后在Activity中给这个Button设置一个onTouchListener
<LinearLayout xmlns:android="http://schemas.a*****.com/apk/res/android" xmlns:tools="http://schemas.a*****.com/tools" 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="com.example.testtouchevent.MainActivity" > <com.example.testtouchevent.MyButton android:id="@+id/bt" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按钮" /> </LinearLayout> public class MainActivity extends Activity { private Button bt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt = (Button) findViewById(R.id.bt); bt.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return false; } }); } }
按下按钮时候,log显式
03-31 11:51:08.493: D/TTTT(1609): context:com.example.testtouchevent.MainActivity@528486ecMyButton|onTouchEvent|return:true|event:DOWN 03-31 11:51:08.493: D/TTTT(1609): context:com.example.testtouchevent.MainActivity@528486ecMyButton|dispatchTouchEvent|return:true|event:DOWN 03-31 11:51:08.493: D/TTTT(1609): ========================== 03-31 11:51:08.493: D/TTTT(1609): context:com.example.testtouchevent.MainActivity@528486ecMyButton|onTouchEvent|return:true|event:DOWN
松开按钮时候,log显式:
03-31 11:51:10.285: D/TTTT(1609): context:com.example.testtouchevent.MainActivity@528486ecMyButton|onTouchEvent|return:true|event:UP 03-31 11:51:10.285: D/TTTT(1609): context:com.example.testtouchevent.MainActivity@528486ecMyButton|dispatchTouchEvent|return:true|event:UP 03-31 11:51:10.285: D/TTTT(1609): ========================== 03-31 11:51:10.289: D/TTTT(1609): context:com.example.testtouchevent.MainActivity@528486ecMyButton|onTouchEvent|return:true|event:UP
可见onTouchEvent方法是在dispatchTouchEvent方法之前执行的
可是View的事件传递机制不是dispatchTouchEvent方法先执行用来传递消息的吗?
请问这是怎么回事儿?