In this article, you will understand the basic concept of spreading touch events and how to intercept them.
The resources that were most helpful in understanding the topic:
What can you implement by reading this article:
__________________ THEORY __________________
Touch event propagation concept:
Now I’m changing your idea of touch events as happened to me.
When a click on View occurs, then first of all this event is processed not by View but Activity and then this event, through the rest of the subsidiaries ViewGroup, gradually reaches the same View.
How it works:
Handling gesture methods:
- dispatchTouchEvent — dispatch touch events to affected child views;
- onInterceptTouchEvent — Intercept touch events before passing to children;
- onTouchEvent — Handle the touch event and stop propogation;
Sequence of method calls:
Note: Nobody processes anything.
He is only in the ViewGroup and his role is very important, he is needed to intercept events (everything seems to be obvious from the name).
The main thing to know about it is that when it returns:
- TRUE — this event will be intercepted, that is, it will be processed by the ViewGroup that overrides the given onInterceptTouchEvent.
- FALSE — this event will go further (to a child element that can handle this event).
This is a special method that only ViewGroup has, it is necessary when we do not want our ViewGroup to intercept events.
Example: You have a vertical ScrollView with child vertical ScrollView (or any component (ViewGroup) that needs to use the vertical scroll (MOVE) event).
Request: What do you think will happen with vertical scrolling through the child component???
Answer: The parent ScrollView will be scrolled!!!
SOLUTION: To fix this and force the child ScrollView to respond to scrolling, you must use requestDisallowTouchIntercept(true). To prevent the parent from intercepting the events of the child.
__________________ PRACTICE _________________
Step 1: Create an empty project
Step 2: Use viewBinding
Step 3: Create class MyConstraintLayout
To override: dispatcherTouchEvent, onInterceptTouchEvent — View/ViewGroup it is necessary to create such classes.
Step 4: Implement activity_main
Step 5: The audience’s narrative about parent and child
Now everything works like this: click on Root displays Root, click on Child displays Child.
But what if we want all events to be listened to by the root???
Step 6: onInterceptTouchEvent | All events are intercepted by the root
As we remember, if the onInterceptTouchEvent returns FALSE, then the event goes to the child who will process this event, if TRUE then processes the ViewGroup itself on which the onInterceptTouchEvent is redefined, since we return TRUE to all events, thereby intercepting all events and the child does not get anything.
— — — — — — -GIT
Step 1: Create Empty Project with viewBinding as in the previous example.
Step 2: Create class MyConstraintLayout
Step 3: Create layout activity_main
Step 4: Create @xml/scene
Step 5: Create class RecyclerAdapter
Step 6: Create layout item
Step 7: Create class MainActivity
Step 8: Complete the class MyMotionLayout
13 lines, only 13 lines solve this problem. (16 to 29)
dispatchTouchEvent — Override it in Activity if you want to catch the touch event before any of the views have a chance to hit it. For ViewGroup (including the root view), just override onInterceptTouchEvent () and onTouchEvent ().
onInterceptTouchEvent — Override if you just want to watch for incoming touch notifications, you can do it here and return false.
However, the main purpose of overriding this method is to allow the ViewGroup to handle a specific type of touch event and a different type for the child.
requestDisallowTouchIntercept — call it if the child does not want its parent to steal the touch event.
Make your application beautiful.
Love what you do and EXPERIMENT !!!!))).