Android Touch event distribution mechanism Learning
Events in Android include buttons and touch events.
A Touch event is an onClick, onLongClick, onScroll event consisting of one ACTION_DOWN, n ACTION_MOVE, and one ACTION_UP.
Methods related to Touch events in Android and their functions:
DispatchTouchEvent (MotionEvent ev) event Distribution
OnInterceptTouchEvent (MotionEvent ev) event Interception
OnTouchEvent (MotionEvent ev) Event Response
Activity: dispatchTouchEvent (MotionEvent ev), onTouchEvent (MotionEvent ev)
View: dispatchTouchEvent (MotionEvent ev), onTouchEvent (MotionEvent ev)
ViewGroup inherits the View. The method above is public, so it owns all the methods above. The onInterceptTouchEvent (MotionEvent ev) method is added to the ViewGroup class ).
To better understand the execution process, I made a demo:
Here we need to know one thing: controls are divided into two types: controls that inherit the View and cannot contain other controls; controls that inherit the ViewGroup and can contain other controls, currently, it is called a container control, such as ListView, GridView, and LinearLayout.
The buttons in the demo above are inherited from the View, while the light green area is a horizontal LinearLayout, And it inherits from the ViewGroup.
First, let's outline how to create a demo:
There are three classes:
MyActivity extends Activity
MyButton extends Button
MyLinearLayout extends LinearLayout
In MyActivity:
public class MyActivity extends Activity{ private static final String TAG = MyActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public boolean dispatchTouchEvent(MotionEvent ev) { Log.i(TAG, MotionEvent.actionToString(ev.getAction()) + dispatchTouchEvent()); return super.dispatchTouchEvent(ev); } public boolean onTouchEvent(MotionEvent event) { Log.i(TAG, MotionEvent.actionToString(event.getAction()) + onTouchEvent()); return super.onTouchEvent(event); }}
MyButton and MyLinearLayout are very similar. MyLinearLayout only has one onInterceptTouchEvent method, and the MyButton code is pasted here. If you need MyLinearLayout code, you can download it directly later to see the complete code.
MyButton
public class MyButton extends Button{ private static final String TAG = MyButton.class.getSimpleName(); 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); // TODO Auto-generated constructor stub } public boolean dispatchTouchEvent(MotionEvent ev) { Log.i(TAG, MotionEvent.actionToString(ev.getAction()) + dispatchTouchEvent()); return super.dispatchTouchEvent(ev); } public boolean onTouchEvent(MotionEvent event) { Log.i(TAG, MotionEvent.actionToString(event.getAction()) + onTouchEvent()); return super.onTouchEvent(event); }}
Java code is very simple
Because we need to use custom controls, the xml layout code is as follows:
Log is displayed as Tag: Specific Class, Text: specific action + call method name
Click MyButton (dark green area). The result is:
Click "MyLinearLayout" in the non-MyButton area (light green area). The result is:
Click the MyActivity non-MyLinearLayout area (blank area). The result is:
Source code, how to deal with it internally.