1.背景
在很多滑动控件嵌套的情况下经常会出现滑动事件冲突等等,在自定义View的时候需要处理触摸,点击,滑动等事件需要考虑父容器的这些事件冲突问题.
2.事件
Activity里面,View里面,ViewGroup里面等
3.View的事件分发
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
android:textAllCaps="false"/>
</RelativeLayout>
public class MainActivity extends AppCompatActivity implements View.OnTouchListener, View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
RelativeLayout parent = findViewById(R.id.parent);
button.setOnTouchListener(this);
parent.setOnTouchListener(this);
button.setOnClickListener(this);
parent.setOnClickListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("MainActivity", "onTouch,View" + v + ",Action:" + event.getAction());
//return false;
return true;
}
@Override
public void onClick(View v) {
Log.d("MainActivity", "onClick,View" + v);
switch (v.getId()) {
case R.id.button:
break;
case R.id.parent:
break;
}
}
}
结论:
1.控件的Listener事件触发的顺序是先onTouch,再onClick。
2.控件的onTouch返回true,将会onClick事件没有了---阻止了事件的传递。
返回false,才会传递onClick事件(才会传递up事件)
分析:
View的事件分发
1.dispatchTouchEvent();
/**
* Pass the touch screen motion event down to the target view, or this
* view if it is the target.
*
* @param event The motion event to be dispatched.
* @return True if the event was handled by the view, false otherwise.
*/
public boolean dispatchTouchEvent(MotionEvent event) {
// If the event should be handled by accessibility focus first.
if (event.isTargetAccessibilityFocus()) {
// We don't have focus or no virtual descendant has it, do not handle the event.
if (!isAccessibilityFocusedViewOrHost()) {
return false;
}
// We have focus and got the event, then use normal event dispatch.
event.setTargetAccessibilityFocus(false);
}
boolean result = false;
if (mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onTouchEvent(event, 0);
}
final int actionMasked = event.getActionMasked();
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Defensive cleanup for new gesture
stopNestedScroll();
}
if (onFilterTouchEventForSecurity(event)) {
if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {
result = true;
}
//noinspection SimplifiableIfStatement
ListenerInfo li = mListenerInfo;
if (li != null && li.mOnTouchListener != null
&& (mViewFlags & ENABLED_MASK) == ENABLED
&& li.mOnTouchListener.onTouch(this, event)) {
result = true;
}
if (!result && onTouchEvent(event)) {
result = true;
}
}
if (!result && mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
}
// Clean up after nested scrolls if this is the end of a gesture;
// also cancel it if we tried an ACTION_DOWN but we didn't want the rest
// of the gesture.
if (actionMasked == MotionEvent.ACTION_UP ||
actionMasked == MotionEvent.ACTION_CANCEL ||
(actionMasked == MotionEvent.ACTION_DOWN && !result)) {
stopNestedScroll();
}
return result;
}
static class ListenerInfo {
/**
* Listener used to dispatch focus change events.
* This field should be made private, so it is hidden from the SDK.
* {@hide}
*/
protected OnFocusChangeListener mOnFocusChangeListener;
/**
* Listeners for layout change events.
*/
private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
protected OnScrollChangeListener mOnScrollChangeListener;
/**
* Listeners for attach events.
*/
private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
/**
* Listener used to dispatch click events.
* This field should be made private, so it is hidden from the SDK.
* {@hide}
*/
public OnClickListener mOnClickListener;
/**
* Listener used to dispatch long click events.
* This field should be made private, so it is hidden from the SDK.
* {@hide}
*/
protected OnLongClickListener mOnLongClickListener;
/**
* Listener used to dispatch context click events. This field should be made private, so it
* is hidden from the SDK.
* {@hide}
*/
protected OnContextClickListener mOnContextClickListener;
/**
* Listener used to build the context menu.
* This field should be made private, so it is hidden from the SDK.
* {@hide}
*/
protected OnCreateContextMenuListener mOnCreateContextMenuListener;
private OnKeyListener mOnKeyListener;
private OnTouchListener mOnTouchListener;
private OnHoverListener mOnHoverListener;
private OnGenericMotionListener mOnGenericMotionListener;
private OnDragListener mOnDragListener;
private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
OnApplyWindowInsetsListener mOnApplyWindowInsetsListener;
OnCapturedPointerListener mOnCapturedPointerListener;
}
ListenerInfo 是一个静态内部类,保存了View身上所有事件的监听,在我们设置监听事件是赋值如果onTouchListener的onTouch方法返回了true,那么view里面的onTouchEvent就不会被调用了.核心代码
//noinspection SimplifiableIfStatement
ListenerInfo li = mListenerInfo;
if (li != null && li.mOnTouchListener != null
&& (mViewFlags & ENABLED_MASK) == ENABLED
&& li.mOnTouchListener.onTouch(this, event)) {
result = true;
}
//onTouchListener的onTouch方法返回了true时,不会走下面代码,当
//onTouchListener的onTouch方法返回了false时,才会走下面代码执行onTouchEvent方法
if (!result && onTouchEvent(event)) {
result = true;
}
}
2.onTouchListener-->onTouch方法
3.onTouchEvent()
4.onClickListener-->onClick方法
结论:
执行顺序
- dispatchTouchEvent-->onTouchListener---return false-->onTouchEvent.
2.如果view为disenable,则:onTouchListener里面不会执行,但是会执onTouchEvent(event)方法
3.onTouchEvent方法中的ACTION_UP分支中触发onclick事件监听,onTouchListener-->onTouch方法返回true,消耗此次事件。down,但是up事件是无法到达onClickListener,onTouchListener-->onTouch方法返回false,不会消耗此事件会响应onClickListenere。