This is enough for Android ViewGroup event distribution

Keywords: Android

Before writing this article, I read the process of event distribution mechanism many times and came from different books, but I still can't remember the process of event distribution.

So I wrote a demo process to analyze event distribution, in order to find the rules of event distribution mechanism and facilitate memory.

Here's the summary rule. If you make a mistake, please report it in time. In the end, some things need to be tried by yourself to understand

Law:

dispatchTouchEvent.   

true does not distribute events to the next level view, stop

false onTouchEvent of an upward Activity

Execute the super method to pass the onintercepttauchevent or onTouchEvent of the same View or the dispatchTouchEvent of the next View

 

onInterceptTouchEvent

true means to intercept and execute the View's onTouchEvent

false and super methods indicate that dispatchTouchEvent is not blocked to execute the next view

 

onTouchEvent

true and super consume this event

false throw event to Activity onTouchEvent

 

Flow chart

Code example:

public class MyTextView extends android.support.v7.widget.AppCompatTextView {

    private static final String TAG = "MyTextView";

    public MyTextView(Context context) {
        super(context);
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        Log.d(TAG,"dispatchTouchEvent");
        return true;
        //return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d(TAG,"onTouchEvent");
        //return true;
        return super.onTouchEvent(event);
    }
}


public class MyLinearLayout extends LinearLayout {

    private static final String TAG = "MyLinearLayout";

    public MyLinearLayout(Context context) {
        super(context);
    }

    public MyLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

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

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.d(TAG,"dispatchTouchEvent");
        //return false;
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.d(TAG,"onInterceptTouchEvent");
        return super.onInterceptTouchEvent(ev);
        //return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d(TAG,"onTouchEvent");
        return super.onTouchEvent(event);
    }
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = findViewById(R.id.mytextview);
        textView.setOnClickListener(this);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.d("MainActivity","dispatchTouchEvent");
        //return false;
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public void onUserInteraction() {
        //Log.d("MainActivity","onUserInteraction");
        //return true;
        super.onUserInteraction();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("MainActivity","onTouchEvent");
        //return true;
        return super.onTouchEvent(event);
    }

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

 

Posted by pp4sale on Thu, 26 Dec 2019 11:44:57 -0800