Article From:https://www.cnblogs.com/ganchuanpu/p/6602432.html

  The default value of onInterceptTouchEvent in Layout is false, so that the touch event is passed to the View control, and the onTouch value in Layout is false by default.ViewThe default return value of onTouch in it is true,When our finger clicks on the screen, the ACTION_DOWN event is called first, and when the return value in onTouch is true, the onTouch will continue to call the ACTION_UP event, if the return value in the onTouch is false, then onToUch will only call ACTION_DOWN instead of ACTION_UP.

xml:

<?xml version="1.0" encoding="utf-8"?>  
<com.tutor.touch.MyLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
     >  
    <com.tutor.touch.MyView  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="@string/hello" />  
</com.tutor.touch.MyLayout>

MyLayout.java

public class MyLayout extends FrameLayout {  

    public MyLayout(Context context){  
        super(context);  
    }  
      
    public MyLayout(Context context, AttributeSet attrs) {  
        super(context, attrs);  
    }  
  
    @Override  
    public boolean onInterceptTouchEvent(MotionEvent ev) {  
        Log.e(TouchDemoActivity.TAG, "MyLayout onInterceptTouchEvent.");  
        Log.e(TouchDemoActivity.TAG,"MyLayout onInterceptTouchEvent default return "   
        + super.onInterceptTouchEvent(ev));  
        return super.onInterceptTouchEvent(ev);  
    }  
      
    @Override  
    public boolean onTouchEvent(MotionEvent event) {  
        Log.e(TouchDemoActivity.TAG, "MyLayout onTouchEvent.");  
        Log.e(TouchDemoActivity.TAG,"MyLayout onTouchEvent default return "   
        + super.onTouchEvent(event));  
        return super.onTouchEvent(event);  
    }  
} 

MyView .java

public class MyView extends Button {  
  
    public MyView(Context context){  
        super(context);  
    }  
      
    public MyView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
    }  
      
    @Override  
    public boolean onTouchEvent(MotionEvent event) {  
        Log.e(TouchDemoActivity.TAG, "MyView onTouchEvent.");  
        Log.e(TouchDemoActivity.TAG,"MyView onTouchEvent default return "   
        + super.onTouchEvent(event));  
        return super.onTouchEvent(event);  
    }  
  

Clicking the red area triggers the onTouch event in MyView.

Clicking the Green Zone triggers the onTouch event in MyLayout, and looks at logcat as follows:

The above two screenshots are the default values of the system, and we can conclude that the default return value of onInterceptTouchEvent is false, and the default return value of onTouchEvent in MyLayout is false, so only ACTION is consumed.In the _DOWN event, the default value of onTouch in MyView is true, calling two times: ACTION_DOW, ACTION_UP.

 

 

 

  

  

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *