Event delivery in Android
When the finger touches the screen, it triggers a touch event, how does the touch event pass through Android, and let's take a look
(1) When a touch event comes, it is first passed to the topmost view, with a top-level view, a level down distribution, and the view that finds the click, passing the message to him
The dispatchtouchevent (motionevent ev) method is called when the event is distributed.
(2) The top-level view can also choose not to distribute this event, but to consume the event itself (then call the Onintercepttouchevent (motionevent ev) method to consume this event), So the child view will not receive this touch event,
This touch event is then handed to the top Ontouchevent (motionevent event) for execution
(3) When the event is passed to the final child view, the child view returns true to indicate that the event has been processed and does not need to be handed over to the parent view for processing.
When the child view returns false, indicating that the child view does not handle the event, the event will be passed up to the parent view for processing, and when the parent view returns FALSE, no one will handle the event.
At this point, this event will only be passed to the child view once, next time will not be passed to the child view
For example, write a class that inherits from ViewGroup
public class Myscrollpager extends viewgroup{
Gesturedetector gesturedetector;//gesture Recognizer
private float FIRSTX = 0;//initial x position
private float firsty = 0;//initial y position
Setting Up Slide monitoring
@Override
public boolean ontouchevent (Motionevent event) {
Super.ontouchevent (event);
Gesturedetector.ontouchevent (event);
Switch (event.getaction ()) {
Case Motionevent.action_down:
Break
Case Motionevent.action_move:
Break
Case MOTIONEVENT.ACTION_UP:
Break
}
return true;
}
/**
* Interrupt of the event,true-> this layer is interrupted and not passed down
*/
public boolean onintercepttouchevent (Motionevent ev) {
boolean bool = false;
Switch (ev.getaction ()) {
Case Motionevent.action_down:
When the event is interrupted at this level, the Ontouchevent method is executed shortly thereafter,
The Action_keydown message will not be received in the Ontouchevent method.
Therefore, the gesture recognizer does not receive the Key_down message, so there is a bounce phenomenon
Gesturedetector.ontouchevent (EV);
FIRSTX = Ev.getx ();
Firsty = Ev.gety ();
Break
Case Motionevent.action_move:
float DISX = Math.Abs (Ev.getx ()-FIRSTX);
float Disy = Math.Abs (Ev.gety ()-firsty);
If the horizontal slide distance is greater than the vertical sliding distance
and the x-axis prevents jitter
if (Disx > Disy && disx > 10) {
BOOL = true;
}else{
bool = false;
}
Break
Case MOTIONEVENT.ACTION_UP:
Break
Default
Break
}
return bool;
}
}
In this method, the Onintercepttouchevent,ontouchevent method is overridden,
These two methods mean that when the finger slides left and right, the event is interrupted, and when the finger slides up and down,
This event is passed in the action_down of Onintercepttouchevent, which
Passed to the gesture recognizer, because when Onintercepttouchevent returns True, this event is passed to its own
The ontouchevent is processed and the Action_down message is not received in the ontouchevent, so onintercepttouchevent
To pass event events to the gesture recognizer.
Event passing mechanism in Android