Let’s start with defining some terms :
The first point where the callback comes is Activity’s
Now, a root view can be of two types :
It evaluates this based on the location coordinates of the touch.
There’s a lot more to this which I’d be explaining in the next post of this series.
Let me in case of any doubts.
Cheers !
- MotionEvent : Every User event in android is wrapped in the MotionEvent class by the framework. It describes user’s current action as well as other metadata such as touch location, no. of pointers and the time when it occurred.
- MotionEvents first flow top down in view hierarchy and then come back to the
rootview
unless they are handled somewhere (explained later) in between. So, a parent always has the control of what to do with an event before sending it to children. - Each view/viewgroup has two functions that are involved in touch handling :
dispatchTouchEvent
: This function is called by a view’s parent to pass it a touch event. The aim of this function is to appropriately route this touch event. It may want to
- consume this event itself : calls it’s own
onTouchEvent
method (explained below). - pass it to the children : calls child’s
dispatchTouchEvent
method. - a listener : calls listener’s
onTouch
method if it has been set.
- consume this event itself : calls it’s own
onTouchEvent
: This is the function where the event processing is done.
- Both
dispatchTouchEvent
andonTouchEvent
return aboolean
. They return true if the event was handled/consumed by them, otherwisefalse
. And we know thatdispatchTouchEvent
either calls child’sdispatchTouchEvent
OR it’s ownonTouchEvent
. So essentially if either of them returntrue
, i.e. consumes the event, it’d returntrue
.
The first point where the callback comes is Activity’s
dispatchTouchEvent
method. This is your opportunity to handle any touch event before it’s passed to anywhere else. After this, the activity passes it to the dispatchTouchEvent
of the root view. Now, a root view can be of two types :
View
: It checks if there’s a touch listener attached and sends the event if it exists. If it’s not there or if it doesn’t consumes
the event, it processes the touch itself i.e. calls its own onTouchEvent
method.ViewGroup
: This view has children to manage. So when it receives this callback, it iterates through the list of children to decide who needs this event. It evaluates this based on the location coordinates of the touch.
- If there is more than one child who qualifies, it iterates over all of them in the reverse order of how they were added.
- It can also steal touch events from the children if it needs to process them. E.g
Scrollview
There’s a lot more to this which I’d be explaining in the next post of this series.
Let me in case of any doubts.
Cheers !
New casino no deposit bonus codes, promos, free spins, slots - Dr
ReplyDeleteCasino No Deposit 오산 출장마사지 Bonuses 2021, Promotions, 포항 출장마사지 Bonus 제주도 출장샵 codes, Free Spins 김해 출장안마 for Real 제주 출장마사지 Money Casino Game Play Online.