This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.

The event distribution mechanism, in short, is Android’s mechanism for the delivery and processing of touch events. The following three methods need to be understood before understanding the distribution mechanism:

  • public boolean dispatchTouchEvent(MotionEvent event)

Used for event distribution. This method must be called if an event is passed to the current View. The return value, influenced by the current View’s onTouchEvent(MotionEvent Event) and the child View’s dispatchTouchEvent(MotionEvent Event) methods, indicates whether the current event is consumed.

  • public boolean onInterceptTouchEvent(MotionEvent ev)

Used to determine whether to intercept the event. The return value indicates whether to intercept the current event.

  • public boolean onTouchEvent(MotionEvent event)

The return value indicates whether the current event is consumed. If not, the current View cannot receive the event again in the same sequence of events.

We know that Android’s View structure is a tree structure, and a View can be placed in a ViewGroup, and that ViewGroup can be placed in a ViewGroup, so when we click on a nested structure, what happens to event passing? To do this, let’s do it in code.

1. Create a BaseViewGroup as the underlying ViewGroup.

public class BaseViewGroup extends FrameLayout {
    public BaseViewGroup(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.d("ViewEvent"."BaseViewGroup-dispatchTouchEvent-"+ev.getAction());
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.d("ViewEvent"."BaseViewGroup-onInterceptTouchEvent-"+ev.getAction());
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("ViewEvent"."BaseViewGroup-onTouchEvent-"+event.getAction());
        return super.onTouchEvent(event); }}Copy the code

Create TopViewGroup as TopViewGroup;

public class TopViewGroup extends FrameLayout {
    public TopViewGroup(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.d("ViewEvent"."TopViewGroup-dispatchTouchEvent-"+ev.getAction());
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.d("ViewEvent"."TopViewGroup-onInterceptTouchEvent-"+ev.getAction());
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("ViewEvent"."TopViewGroup-onTouchEvent-"+event.getAction());
        return super.onTouchEvent(event); }}Copy the code

Create MyView as the topmost View.

public class MyView extends View {
    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.d("ViewEvent"."MyView-dispatchTouchEvent-"+ev.getAction());
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("ViewEvent"."MyView-onTouchEvent-"+event.getAction());
        return super.onTouchEvent(event); }}Copy the code

4. Layout custom View and ViewGroup, code as follows:


      
<com.droidyu.viewsystem._3_event.BaseViewGroup 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:layout_width="400dp"
    android:layout_height="400dp"
    android:background="#1f1"
    tools:context="._3_event.ViewEventActivity">

    <com.droidyu.viewsystem._3_event.TopViewGroup
        android:background="#ff1"
        android:layout_width="300dp"
        android:layout_height="300dp" >

        <com.droidyu.viewsystem._3_event.MyView
            android:background="#f11"
            android:layout_width="200dp"
            android:layout_height="200dp" />
    </com.droidyu.viewsystem._3_event.TopViewGroup>

</com.droidyu.viewsystem._3_event.BaseViewGroup>
Copy the code

The final view structure looks like this:

Run the program and click on the red oneMyViewAnd then checkLogThe log is as follows:LogThe logs show that under normal circumstances,

Events are delivered in the following order: BaseViewGroup -> TopViewGroup -> MyView’s dispatchTouchEvent and onInterceptTouchEvent methods

The event processing sequence is: MyView -> TopViewGroup -> BaseViewGroup onTouchEvent method

At this time will beBaseViewGrouptheonInterceptTouchEventReturn value changed totrueClick the red one againMyViewAnd then checkLogThe log is as follows:LogAs you can see from the logs,BaseViewGroupAfter the event is intercepted, the event is processed directly and no more event is sent to the childViewPass.

Restore the code toTopViewGrouptheonInterceptTouchEventReturn value changed totrueClick the red one againMyViewAnd then checkLogThe log is as follows:LogLogs can be seen from eventsBaseViewGroupPassed to theTopViewGroup.TopViewGroupAfter the event is intercepted, the event is processed directly and no more event is sent to the childViewPass, processing will not consume, returned toBaseViewGroupTo deal with.

Restore the code toMyViewtheonTouchEventReturn value changed totrueClick the red one againMyViewAnd then checkLogThe log is as follows:LogLogs show that events are passed toMyView.MyViewAfter the event is processed, it is no longer passed up.

Restore the code toTopViewGrouptheonTouchEventReturn value changed totrueClick the red one againMyViewAnd then checkLogThe log is as follows:LogLogs show that events are passed toMyView.MyViewAfter the event is processed, it is passed up toTopViewGroupTo deal with,TopViewGroupAfter the event is processed, it is no longer passed up.

By now you have a more intuitive understanding of View event passing and handling. More explanation will be updated in the next article, please look forward to…

Source see lot

To learn more, please check out my personal blog:droidYu