Code Xiaosheng, a technology platform focused on the Android field

Join my Android technology group

Author: door heart diao dragon link: https://www.jianshu.com/p/b173ada7b496 disclaimer: this article published an authorised person of door heart diao dragon, forwarding etc. Please contact the author

This article summarizes some common problems in the actual Android development process over the years, and now share the hope to help beginners.

This article is from the blog of Men Xin Diao Long, please note the source of reprint:

https://blog.csdn.net/geduo_83/article/details/86560896

Event distribution is the focus of Android development process is difficult, an event distribution flow chart, let you thoroughly understand. There are many articles on the Internet to write events to distribute, feel not clear, congratulations, today you see a good article, you thoroughly understand…

directory

[1. What are the classes with event passing function in Android operating system?] [2. Type of touch event?] [3. Three stages of event transmission?] [4. Describe the event passing mechanism of View.] [5. Describe the event passing mechanism of ViewGroup.] [6. Event distribution flow chart][7. Actual case]

1. In the Android operating system, what are the classes with event passing function?

  • Activity: Has dispathTouchEvent and onTouchEvent methods

  • View: Has dispathTouchEvent and onTouchEvent methods

  • ViewGroup: has dispatchTouchEvent, onTouchEvent, onInterceptTouchEvent

2. What type of touch event?

There are three main types:

  • ACTION_DOWN: Finger pressing

  • ACTION_MOVE: Indicates the event triggered by a slight movement after the finger is pressed and before the hand is released

  • ACTION_UP: Action of moving fingers away from the screen

3. Three stages of event transmission?

3.1 Classification by event

image

3.2 Divide according to View

image

Conclusion: Whether it’s a View or a ViewGroup, whether it’s a DispatchTouchEvent method or an onTouchEvent method, the logic that returns true and false is the same, except that the logic is different when calling the parent method of the same name. View emphasizes consumption and ViewGourp emphasizes distribution

4. Describe the event passing mechanism of View.

The process of delivering touch events starts with dispatchTouchEvent. If there is no manual intervention, the event will be passed from outer layer to inner layer according to the nested level of the View tree. When it reaches the innermost View, it will be handled by its onTouchVent method

If the event is manually intervened during transmission, the event distribution function returns true to indicate that it consumes the event itself, returns a method of the same name as the parent class, and passes the event to its own onTouchEvent for processing, and returns false to indicate that the event is passed back to the parent View’s onTouchEvent method for processing. At this point, all subsequent events cannot be accepted, which View will handle the last, all subsequent events will be handled by it

If an event is passed manually, the event handler returns true and calls a method of the same name to indicate that the event is consumed, and returns false to indicate that the event is passed back to the parent class’s method of the same name for processing

If the onTouch method returns tue, the event will be consumed and onClick will not be called. If the onTouch method returns false, the event will continue and onClick will be called

5. Describe the event passing mechanism of ViewGroup.

Touch events are passed from the Activity to the ViewGroup, and recursively from the ViewGroup to its child views. The ViewGroup intercepts the event via the onInterceptTouchEvent method. If this method returns true, The event will not continue to pass down to the View, if false or call super. OnInterceptTouchEvent, the event will continue to be passed to the View

If the event is manually intervened during transmission, the event distribution function returns true to indicate that the event was consumed by itself, and returns false, the onTouchEvent passed back to the parent View will be processed. At this point, all subsequent events will not be accepted, and the event will continue to be passed by calling the method with the same name

If an event is passed manually, the event handler returns true to indicate that the event is consumed, and returns false and calls a method of the same name to indicate that the event is passed back to the parent class for processing

6. Event distribution flow chart

Android event distribution process, let you thoroughly understand the Android event distribution mechanism

image

7. Real case study

The result is as follows, the bottom of the trip details can be dragged up to cover the map, can also be dragged down to stop in the middle of the screen, map related operations: zoom in, zoom out, move can be normal response, how to achieve? The following is the specific source code implementation process

image

7.1 Event Distribution and Processing

mTransparentView = findViewById(R.id.view_tansparent); mTransparentView.setListener(new TransparentView.TouchEventListener() { @Override public boolean dispatchTouchEvent(MotionEvent event) { return mMapView.dispatchTouchEvent(event); }}); mScrollView = findViewById(R.id.view_scrollview); mScrollView.setListener(new TransparentView.TouchEventListener() { @Override public boolean dispatchTouchEvent(MotionEvent event) { Rect rect = new Rect(); mTransparentView.getLocalVisibleRect(rect); if(rect.contains((int)event.getX(),(int)event.getY())){ return true; }else{ return false; }}});Copy the code

7.2 Customizing ViewTransparentView

public class TransparentView extends View { TouchEventListener mListener; public interface TouchEventListener{ boolean dispatchTouchEvent(MotionEvent event); } public TransparentView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } @Override public boolean dispatchTouchEvent(MotionEvent event) { if(mListener ! = null){ return mListener.dispatchTouchEvent(event); }else{ return super.dispatchTouchEvent(event); } } public void setListener(TouchEventListener listener) { mListener = listener; }}Copy the code

7.3 Customizing a TransScrollView

public class TransScrollView extends NestedScrollView { public TransparentView.TouchEventListener mListener; public TransScrollView(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (mListener ! = null && mListener.dispatchTouchEvent(ev)) { return false; } return super.onInterceptTouchEvent(ev); } public void setListener(TransparentView.TouchEventListener listener) { mListener = listener; }}Copy the code

7.4 Layout File

<FrameLayout    android:layout_width="match_parent"    android:layout_height="match_parent"    >    <com.amap.api.maps.MapView        android:id="@+id/map"        android:layout_width="match_parent"        android:layout_height="250dp"        />    <com.zhijiaxing.travel.trip.record.view.TransScrollView        android:id="@+id/view_scrollview"        android:layout_width="match_parent"        android:layout_height="match_parent"        >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        >        <com.zhijiaxing.travel.trip.record.view.TransparentView            android:id="@+id/view_tansparent"            android:layout_width="match_parent"            android:layout_height="250dp"            android:background="#00000000"            />        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="#ffffff"            android:orientation="vertical"            >        </LinearLayout>    </LinearLayout>    </com.zhijiaxing.travel.trip.record.view.TransScrollView></FrameLayout>Copy the code

Recommended reading

Learn the Android event distribution mechanism from another idea

The key thinking of architectural design is

Judgment and trade-offs