android事件分发默认流程
主要函数
dispatchTouchEvent
onInterceptTouchEvent
onTouchEvent
- 事件传递流程
activity—->viewgroup—->view—-> viewgroup—-> activity—-over
如下图
分发总结
事件分发的方法都会返回boolean值,有三种状态 super. true false
activity dispatchTouchEvent返回false会直接走activity的onTouchEvent 然后结束1.返回super. 就是默认分发顺序 如上图;
2.返回true时很好理解 就是本次事件被消费掉了(onInterceptTouchEvent除外) ,意思就是没有了 不会再继续传递;
3.返回false ,view总dispatchTouchEvent 返回false拦截不会向下分发会直接回调上级控件(viewgroup)的onTouchEvent ,接下来跟正常流程走.viewgroup dispatchTouchEvent的返回false会回调activity的onTouchEvent;
4.viewgroup的onInterceptTouchEvent函数比较特殊,返回true会直接掉自己的onTouchEvent返回false的话会走正常分发流程;
代码验证
- ---- viewgroup
public class MLinearlayout extends LinearLayout{
public MLinearlayout(Context context) {
super(context);
}
public MLinearlayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.i("MLinearlayout","onInterceptTouchEvent");
return false;
// return super.onInterceptTouchEvent(ev);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.i("MLinearlayout","dispatchTouchEvent");
// return true;
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i("MLinearlayout","onTouchEvent");
return super.onTouchEvent(event);
}
}
/**
* @author luki
* @create 2018/4/19 下午2:20
* @function :
*/
-----view
public class MImage extends AppCompatImageView {
public MImage(Context context) {
super(context);
}
public MImage(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.i("MImage--------","dispatchTouchEvent");
// return false;
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i("MImage--------","onTouchEvent");
return super.onTouchEvent(event);
}
}
--------activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical"
android:padding="10dp">
<view.MLinearlayout
android:id="@+id/m_linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/khaki"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/btn_i"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是一个按钮" />
<view.MImage
android:id="@+id/m_imag"
android:layout_width="300dp"
android:layout_height="100dp"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher" />
</view.MLinearlayout>
</LinearLayout>
/**
* @author luki
* @create 2018/2/28 下午5:17
* @function :
*/
public class ActionTestActivity extends AppCompatActivity {
@BindView(R.id.btn_i)
Button btnI;
@BindView(R.id.m_linear)
MLinearlayout mLinear;
@BindView(R.id.m_imag)
MImage m_imag;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_aciton);
ButterKnife.bind(this);
initData();
}
public void initData() {
mLinear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("MLinearlayout","OnClickListener");
}
});
mLinear.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("MLinearlayout","setOnTouchListener");
return false;
}
});
// btnI.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// Log.i("btn onClick","OnClickListener");
// }
// });
//
// m_imag.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// Log.i("m_imag onClick","OnClickListener");
// }
// });
}
补充一点viewgroup view 如果设置onTouch 事件 onTouch的回调会在onTouchEvent之前 原理可以去看 dispatchTouchEvent 方法的源码 他们调用 有先后顺序;