After the holiday, we will have a holiday. Wow, Haha, I wish you a happy Dragon Boat Festival and eat more dumplings. The company sent us a few dumplings. I had a good meal this morning, back to the topic... I have read a lot about this on the Internet.ArticleSome of them write well, and I decided to write an article like this myself. First, I had to understand the relationship between onintercepttouchevent and ontouchevent, second, I hope you will see that I can write less detours, and my language is not very good. The language is taught by a mathematics teacher. Every time I write an article, I will follow the feeling.
1. onintercepttouchevent (), intercept blocking, interrupt meaning, as the name suggests, this method is to block the delivery of touchevent, the return value of this method is false or true, maybe many people will ask, what does it mean to block the delivery of touchevent? Onintercepttouchevent () is the method in viewgroup. A viewgroup can contain many viewgroups and views, and the view in viewgroup must be transmitted from viewgroup to obtain the touchevent, the onintercepttouchevent method intercepts touchevent. As the name suggests, when onintercepttouchevent returns false, the Child view has a touchevent. When false is returned, the touchevent of the child view is intercepted, the touchevent is processed by the viewgroup, and the Child view has no touchevent.
2. ontouchevent () is used to process events (the important ontouch event is uploaded from the Child control back to the parent control, and is uploaded layer by layer). The returned value determines whether the current control consumes the event (consume, that is to say, whether the touch event can be passed up (parent Control) after the current control finishes processing the touch event.
If false is returned, it is passed up to the parent control. For details, the touch event is sent to the parent control, and the up event is triggered by touch here, it will not be passed to its child control. If the parent control is still false, touch's processing will be sent to the parent control of the parent control, so the up event processing will be in the parent control of the parent control, and will not trigger the following.
Returns true. If the child control returns true, all its touch events are processed here. The parent control cannot handle this because it cannot receive the touch that the child control sends to it, quilt control intercepted
(Note: Do you think it is related to consumption? Anyway, I have compiled a processing method for the event.Code? The answer is yes! For example, the premise that action_move or action_up occurs is that action_down has occurred. If you have not consumed action_down, the system will think that action_down has not happened, so action_move or action_up cannot be captured .)
The following uses examples to deeply understand the relationship between the two.
1. First, we create a new mylayout that inherits linearlayout. We know thatOnintercepttouchevent () is the method in viewgroup. We do not directly inheritViewgroup inherits an export class (subclass) of viewgroup, And Then override the onintercepttouchevent and ontouchevent methods,Onintercepttouchevent returns false by default, that is, it does not interceptToucheventOntouchevent returns false by default, that is, viewgroup is not consumed by default.Touchevent
Package COM. example. ontouchactivity; import android. content. context; import android. util. attributeset; import android. util. log; import android. view. motionevent; import android. widget. linearlayout; public class mylayout extends linearlayout {Private Static final string tag = "ontouch"; Public mylayout (context, attributeset attrs) {super (context, attrs );} @ overridepublic Boolean onintercepttouchevent (motionevent eV) {log. E (TAG, "mylayout ---- onintercepttouchevent"); return Super. onintercepttouchevent (EV);} @ overridepublic Boolean ontouchevent (motionevent event) {Switch (event. getaction () {Case motionevent. action_down: log. E (TAG, "mylayout ---- down"); break; Case motionevent. action_up: log. E (TAG, "mylayout ---- up"); break;} return Super. ontouchevent (event );}}
Then layout the file and nest a textview in mylayout.
<COM. example. ontouchactivity. mylayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: Id = "@ + ID/text" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: layout_centerhorizontal = "true" Android: layout_centervertical = "true" Android: TEXT = "@ string/hello_world"/> </COM. example. ontouchactivity. mylayout>
Mainactivity is as follows:
Package COM. example. ontouchactivity; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. motionevent; import android. view. view; import android. view. view. ontouchlistener; import android. widget. textview; public class mainactivity extends activity {Private Static final string tag = "ontouch"; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); textview mtextview = (textview) findviewbyid (R. id. text); mtextview. setontouchlistener (New ontouchlistener () {@ overridepublic Boolean ontouch (view V, motionevent event) {Switch (event. getaction () {Case motionevent. action_down: log. I (TAG, "textview ---- down"); break; Case motionevent. action_up: log. I (TAG, "textview ---- up"); break;} return false ;}});}}
1. By default, we can touch the screen to see the print
transfer order: mylayout ----> textview -----> mylayout, because onintercepttouchevent returns false by default, that is, the touchevent is not blocked, so the touchevent is passed to textview, textview returns false, that is, touchevent is not consumed, and touchevent is passed up to mylayout , ontouchevent is returned by default as false, that is, it does not consume touchevent, so we can see the print / span>
2. modify onintercepttouchevent return value of mylayout to true to intercept touchevent, view the output
>
transfer order: mylayout -----> mylayout. We can see touchevent is not passed to textview, but is blocked by mylayout. After the interception, mylayout will send touchevent to yourself,
and the ontouchevent of mylayout returns false by default, that is, the touchevent is not consumed, so we can see the above print
>
3. modify the value of onintercepttouchevent returned by mylayout to true to intercept touchevent, change the ontouchevent return value of mylayout to true to display the output.
>
delivery sequence: mylayout -----> mylayout ----> mylayout , touchevent is intercepted by mylayout. touchevent is passed to you, the ontouchevent returns true, that is, you can consume touchevent, so we can see the above print
4. Set mylayout'sThe onintercepttouchevent return value is changed to false, and the ontouch return value of textview is changed to true. It doesn't matter if the ontouchevent return value of mylayout is false or true.
Transfer order: PressMylayout -----> textview, liftMylayout -----> textview, because mylayoutTouchevent is passed to textview, while textview returns true and consumesTouchevent, consumedTouchevent will not be passed to mylayout. The same is true for raising the touchevent.
This is probably the meaning. I don't know if you can understand the articles written according to my ideas. I hope you can point out the incorrect articles. Thank you.