ViewGroup事件分發(fā)

廢話不說先上代碼

布局文件

<?xml version="1.0" encoding="utf-8"?>
<com.example.cc.eventdemo.MyRelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/parent"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.cc.eventdemo.MainActivity">

 <Button
      android:id="@+id/button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="button"
      android:textAllCaps="false"/>
  <com.example.cc.eventdemo.MyButton
    android:id="@+id/mybtn"
    android:textAllCaps="false"
    android:text="mybutton"
    android:layout_width="wrap_content"        
    android:layout_height="wrap_content"
    android:layout_centerInParent="true">
   </com.example.cc.eventdemo.MyButton>
 </com.example.cc.eventdemo.MyRelativeLayout>

兩個(gè)自定義view的代碼

自定義MyButton

  public class MyButton extends Button {

 public MyButton(Context context) {
      this(context,null);
  }
public MyButton(Context context, AttributeSet attrs) {
    this(context, attrs,0);
}
public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    Log.d("123", "dispatchTouchEvent:MyButton: "+event.getAction());
    return super.dispatchTouchEvent(event);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.d("123", "onTouchEvent: MyButton:"+event.getAction());
    return  super.onTouchEvent(event);
}
}

自定義RelativeLayout

 public class MyRelativeLayout extends RelativeLayout {
public MyRelativeLayout(Context context) {
    super(context);
}

public MyRelativeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}


@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    Log.d("123", "dispatchTouchEvent: MyRelativeLayout:"+ev.getAction());
    return super.dispatchTouchEvent(ev);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    Log.d("123", "onInterceptTouchEvent: MyRelativeLayout:"+ev.getAction());
    return super.onInterceptTouchEvent(ev);
}


@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.d("123", "onTouchEvent: MyRelativeLayout:"+event.getAction());
    return super.onTouchEvent(event);
}

}

MainActivity代碼

public class MainActivity extends AppCompatActivity implements View.OnTouchListener, View.OnClickListener {

private MyButton mMyButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = findViewById(R.id.button);

    MyRelativeLayout parent = findViewById(R.id.parent);

    mMyButton = findViewById(R.id.mybtn);

    button.setOnTouchListener(this);
    parent.setOnTouchListener(this);

    button.setOnClickListener(this);
    parent.setOnClickListener(this);



    mMyButton.setOnTouchListener(this);
    mMyButton.setOnClickListener(this);

}

@Override
public boolean onTouch(View v, MotionEvent event) {

    Log.d("123", "onTouch,View" + v + ",Action:" + event.getAction());

    return false;
}

@Override
public void onClick(View v) {
    Log.d("123", "onClick,View" + v);
}

}

點(diǎn)擊mybutton運(yùn)行打印結(jié)果:


image.png

結(jié)論:父容器先拿到down事件分別調(diào)用dispatchTouchEvent向下分發(fā),onInterceptTouchEvent攔截事件此時(shí)不攔截,向子view(MyButton)分發(fā)down事件,MyButton獲取down事件調(diào)用onTouch,再調(diào)onTouchEvent方法響應(yīng)down事件.當(dāng)松開手指父容器先獲取up事件,執(zhí)行順序跟down事件一樣,如果MyButton設(shè)置了點(diǎn)擊事件會調(diào)用onClick

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容