Http://blog.csdn.net/nexttake/article/details/8243981
Test Project:
Touchtest.zip
Http://download.csdn.net/detail/victoryckl/5005142
First, read the official Android documentation.
Onintercepttouchevent () and ontouchevent () mechanisms:
1. The down event is first passed to the onintercepttouchevent () method.
2. if the onintercepttouchevent () of the viewgroup receives the return false after the down event processing is completed, the subsequent move, up and other events will be passed to the viewgroup first, the ontouchevent () process is passed to the final target view just like the down event.
3. if the onintercepttouchevent () of the viewgroup returns true after it receives the down event processing, the subsequent move, up and other events will not be passed to onintercepttouchevent (), the ontouchevent () that is passed to the viewgroup like the down event. Note that the target view will not receive any event.
4. If the ontouchevent () of the view that finally needs to process the event returns false, the event will be passed to the ontouchevent () processing of the view at the previous level.
5. If the ontouchevent () of the view that finally needs to process the event returns true, subsequent events can be passed to the ontouchevent () of the view for processing.
The previous view only has the parent-child relationship between la S and does not have any relationship with inheritance.
By reading the explanation in this official document, you can understand the relationship between the two functions and their usage. It is definitely an experienced framework expert.
Otherwise, a case must be explained. Suppose we have such a layout, which is very typical.
- <com.test.LayoutView1 xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <com.test.LayoutView2
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent" android:gravity="center">
- <com.test.MyTextView
- android:layout_width="wrap_content" android:layout_height="wrap_content"
- />
- </com.test.LayoutView2>
- </com.test.LayoutView1>
Use an example image to explain this layout:
Normally, the peripheral layoutview1 and layoutview2, but the layout container does not need to respond to the touch screen Click Event, and only the mytextview needs to be clicked accordingly. However, this is only common. Some special la S may also require responses from peripheral containers, or even prevent the mytextview from responding. A special case is that the response object is changed dynamically.
First, let's take a look at the transfer process of the default touch screen event between two functions. For example:
If you only want mytextview to respond to touch-screen events and make the ontouchevent of mytextview return true, the event stream becomes as shown in. You can see that layoutview1 and layoutview2 cannot enter ontouchevent:
In another case, if the peripheral container wants to handle touch-screen events on its own, it should return true in the corresponding onintercepttouchevent function to capture touch-screen events, such as layoutview1 for interception, the processing stream is changed:
Similarly, we can obtain various specific situations. The View class layers of layout have the opportunity to intercept, and we can see that the view of the peripheral container has the priority to intercept.
When we do some applications with more complex touch-screen interaction effects, we often need to dynamically change the processing objects of touch events, such as launcher's standby desktop and main menu (SEE ), from sliding the screen to stopping the sliding process, only the peripheral container view can process the touch event. Otherwise, the application icon or widget above will be accidentally clicked. otherwise, you must be able to respond to the touch event of the icon (sub view) in the static state. The code for extracting abslistview from the framework is as follows:
- Public Boolean onintercepttouchevent (motionevent eV ){
- Int action = eV. getaction ();
- Switch (Action & motionevent. action_mask ){
- Case motionevent. action_down :{
-
- If (touchmode = touch_mode_fling ){
- Return true; // fling status. Touch is intercepted because the sub-view is not processed in the sliding status.
- }
- Break;
- }
- Case motionevent. action_move :{
- Switch (mtouchmode ){
- Case touch_mode_down:
- Final int pointerindex = eV. findpointerindex (mactivepointerid );
- Final int y = (INT) eV. Gety (pointerindex );
- If (startscrollifneeded (Y-mmotiony )){
- Return true; // start to slide the status. The touch event is intercepted and not processed by the subview.
- }
- Break;
- }
- Break;
- }
- }
Summary:
It is difficult to understand the purpose of the onintercepttouchevent function only through the official overview document. This important knowledge can be obtained only by interpreting this abstract rule and matching the image and text. Obviously, the default value is false without interception. After true is returned, the backend control of the event stream has no chance to process the touch event. Each processing function in the default event stream is considered as a node. If this node returns true, the subsequent events are terminated, so you can understand them well.