|
In Android, the transfer of control messages mainly through two ways to distribute together with the use of the user's touch message, take a look at the following two methods;
onInterceptTouchEvent: This method is defined in ViewGroup the name suggests, this method is a touch message for ViewGroup intercept (intercept) of;
onTouchEvent: This method is defined in the View, and touch for handling user events;
Let's look at the definition of prototypes of these two methods;
public boolean onInterceptTouchEvent (MotionEvent ev);
public boolean onTouchEvent (MotionEvent event);
Specific information has in common is a parameter of these two methods for obtaining MotionEvent touch events (such as pressing, mobile messaging, etc. morphology) and function return values (for controlling the process touch on different occasions);
Briefly about MotionEvent, it contains the type of the user touches the message, several commonly used touch message type: ACTION_DOWN, ACTION_MOVE, ACTION_UP, ACTION_CANCEL, respectively touch pressed, moving to end state; these types of message type is not concurrent generated, but the same as the order of the touch occurrence ordered produced, DOWN-> MOVE-> UP / CANCEL;
Below an example to study the custom of a simple ViewGroup and View, and onInterceptTouchEvent onTouchEvent for tracking implementation;
LLinearLayout
public class LLinearLayout extends LinearLayout {
public LLinearLayout (Context context, AttributeSet attrs) {
super (context, attrs);
}
public LLinearLayout (Context context) {
super (context);
}
private float lastY;
@Override
public boolean onInterceptTouchEvent (MotionEvent ev) {
String tag = "onInterceptTouchEvent";
Log.w (tag, "" + super.onInterceptTouchEvent (ev));
switch (ev.getAction ()) {
case MotionEvent.ACTION_DOWN:
Log.w (tag, "ACTION_DOWN");
lastY = ev.getX ();
break;
case MotionEvent.ACTION_MOVE:
Log.w (tag, "ACTION_MOVE");
if (ev.getX () - lastY> 20) {
Log.w (tag, "ACTION_MOVE> 20");
return true;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
Log.w (tag, "ACTION_UP");
break;
}
return false;
}
@Override
public boolean onTouchEvent (MotionEvent event) {
Log.w ( "onTouchEvent", "" + super.onTouchEvent (event));
return false;
}
}
LView
public class LView extends ImageView {
public LView (Context context, AttributeSet attrs) {
super (context, attrs);
}
public LView (Context context) {
super (context);
}
@Override
public boolean onTouchEvent (MotionEvent event) {
String tag = "LView.onTouchEvent";
Log.e (tag, "" + super.onTouchEvent (event));
switch (event.getAction ()) {
case MotionEvent.ACTION_DOWN:
Log.e (tag, "ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.e (tag, "ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
Log.e (tag, "ACTION_UP");
break;
}
return false;
}
}
xml file will be added in LView LLinearLayout, change to a series of control exactly;
step1:
Log by the above information, we can see, touch event is first LLinearLayout intercepted (onInterceptTouchEvent), in this Demo, the method returns a value of false, then it will be passed to the event Touch LView, LView of onTouchEvent responded to the touch events, and also returns false; then Touch event before being passed to the onTouchEvent LLinearLayout processing;
We can easily be seen from the above, Touch event was first LLinearLayout intercepted and passed to LView, LView perform onTouchEvent processing logic; then LLinearLayout then perform their onTouchEvent processing logic; @ 1
step2: the onInterceptTouchEvent above method LLinearLayout return value to true, run the program again;
Log compared to the above information, we can see that the message was not delivered to LView; here, you should be able to draw a conclusion a small bar;
ViewGroup inside onInterceptTouchEvent return value Returns true if the event interception Touch, Touch event will no longer be passed to the child inside ViewGroup View;
step3: Back step1, will LView in onTouchEvent return value to true, run the program again, slide your finger from the screen left and right;
From the Log information can be seen, LView of onTouchEvent returns true when, LView touch events passed from DOWN to MOVE, and then passed to UP; of course, the whole process is the first by LLinearLayout onInterceptTouchEvent first received Touch event here and no interceptions Touch event, but the event is passed to the child Touch View; careful friends may find, here, did not perform to the onTouchEvent LLinearLayout method, why in fact, because of onTouchEvent LView event returns true? represents processing consumption of this event, no longer continue to pass, and not to perform the onTouchEvent LLinearLayout method;
Conclusion: View of onTouchEvent return value indicates whether or not to pass on Touch events, such as If true, the form will be transmitted from the touch to MOVE DOWN then UP (where this argument is not really rigorous, here refers to the entire onTouchEvent method return value It is true, and no segment is returned, such as when MOVE forms returned false);
step4: Continue step3, run the program, slide your finger from the screen left and right;
In LLinearLayout of onInterceptTouchEvent if MOVE message to the right sliding distance is greater than 20, then the interception Touch events, so after the event is intercepted, it will not be like step3, the MOVE message will continue to be passed LView of onTouchEvent, whereas is interrupted, touching a finger that is morphological changes to ACTION_UP lift (in fact, this is wrong to say, that is accurate touch morphological changes ACTION_CANCEL, because I will CANCEL uP and treated as a class, as shown above, print it out the Log information ACTION_UP, in fact it is essentially ACTION_CANCEL); then due ACTION_MOVE was blocked, so LLinearLayout of onTouchEevent continue to be invoked when the finger MOVE;
The end of the summary:
ViewGroup Lane onInterceptTouchEvent default value is false, only if the return value is false when Touch event was passed to son View, and then call the child's View onTouchEvent; returns true when the user will intercept Touch event, the sub-View not catch touch events;
View of onTouchEvent method, when the return value is true, the event will continue to be passed down, and then delivered by ACTION_DOWN to ACTION_MOVE ACTION_UP, whereas if the return value is false, only the capture of MotionEvent ACTION_DOWN form; |
|
|
|