View的事件分发

View的事件分发分View和ViewGroup的事件分发

View的事件分发

  1. View的事件分发主要是三个方法
* dispatchTouchEvent()
* onTouchListener()
* onTouchEvent()

自定义button打印相关log

public class MyButton extends android.support.v7.widget.AppCompatButton {

  private static final String TAG = "MyButton";
  public MyButton(Context context) {
      super(context);
  }

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

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

  @Override
  public boolean onTouchEvent(MotionEvent event) {
      switch (event.getAction()){
          case MotionEvent.ACTION_DOWN:
              KLog.d(TAG, "onTouchEvent: ACTION_DOWN");
              break;
          case MotionEvent.ACTION_MOVE:
              KLog.d(TAG, "onTouchEvent: ACTION_MOVE");
              break;

          case MotionEvent.ACTION_UP:
              KLog.d(TAG, "onTouchEvent: ACTION_UP");
              break;
      }
      return super.onTouchEvent(event);
  }

  @Override
  public boolean dispatchTouchEvent(MotionEvent event) {
      switch (event.getAction()){
          case MotionEvent.ACTION_DOWN:
              KLog.d(TAG, "dispatchTouchEvent: ACTION_DOWN");
              break;
          case MotionEvent.ACTION_MOVE:
              KLog.d(TAG, "dispatchTouchEvent: ACTION_MOVE");
              break;

          case MotionEvent.ACTION_UP:
              KLog.d(TAG, "dispatchTouchEvent: ACTION_UP");
              break;
      }
      return super.dispatchTouchEvent(event);
  }
}

主界面

public class MainActivity extends AppCompatActivity {
  private static final String TAG = "MyButton";
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      findViewById(R.id.btn).setOnTouchListener(new View.OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
              switch (event.getAction()) {
                  case MotionEvent.ACTION_DOWN:
                      KLog.d(TAG, "onTouch: ACTION_DOWN");
                      break;
                  case MotionEvent.ACTION_MOVE:
                      KLog.d(TAG, "onTouch: ACTION_MOVE");
                      break;
                  case MotionEvent.ACTION_UP:
                      KLog.d(TAG, "onTouch: ACTION_UP");
                      break;
              }
              return false;
          }
      });
  }
}
输出log:
06-02 13:19:31.677 3119-3119/com.robert.touchevent D/MyButton: [ (MyButton.java:49)#dispatchTouchEvent ] dispatchTouchEvent: ACTION_DOWN
06-02 13:19:31.677 3119-3119/com.robert.touchevent D/MyButton: [ (MainActivity.java:21)#onTouch ] onTouch: ACTION_DOWN
06-02 13:19:31.678 3119-3119/com.robert.touchevent D/MyButton: [ (MyButton.java:32)#onTouchEvent ] onTouchEvent: ACTION_DOWN
06-02 13:19:32.070 3119-3119/com.robert.touchevent D/MyButton: [ (MyButton.java:52)#dispatchTouchEvent ] dispatchTouchEvent: ACTION_MOVE
06-02 13:19:32.070 3119-3119/com.robert.touchevent D/MyButton: [ (MainActivity.java:24)#onTouch ] onTouch: ACTION_MOVE
06-02 13:19:32.071 3119-3119/com.robert.touchevent D/MyButton: [ (MyButton.java:35)#onTouchEvent ] onTouchEvent: ACTION_MOVE
06-02 13:19:32.308 3119-3119/com.robert.touchevent D/MyButton: [ (MyButton.java:56)#dispatchTouchEvent ] dispatchTouchEvent: ACTION_UP
06-02 13:19:32.309 3119-3119/com.robert.touchevent D/MyButton: [ (MainActivity.java:28)#onTouch ] onTouch: ACTION_UP
06-02 13:19:32.309 3119-3119/com.robert.touchevent D/MyButton: [ (MyButton.java:39)#onTouchEvent ] onTouchEvent: ACTION_UP

根据写代码测试可得:
dispatchTouchEvent()->onTouchListener()(是要判断是否设置过,或者当前View是否Enable)->onTouchEvent();

ViewGroup的事件分发

  1. ViewGroup的事件分发主要三个方法:
  • dispatchTouchEvent()
  • onInterceptTouchEvent()
  • onTouchEvent()

自定义LinearLayout打印相关事件的触发

你可能感兴趣的:(View的事件分发)