Need for ViewParent.disallowInterceptTouchEvent? - android

Views may at any time invoke disallowInterceptTouchEvent() which will prevent parent views from handling touch events. In short, what is the need for this API? I see that lists use it when a scroll is initiated, but why? The list would still receive ACTION_CANCEL if a parent view intercepts the event, so why would it specifically want to block onInterceptTouchEvent on parent views?

Related

Android OnTouchEvent and onclick event

I am developing an app, which contains several fragments just like the picture below:
Picture 1
The bottom view is MainActivity, and there are several fragments above MainActivity, which may contain buttons. I want to add a fragment on top in order to listen the OnTouchEvents (gestures), but ignore the onclick events and pass the onclick events to the fragments below.
Here are my questions,
How can I distinguish onTouchEvent and onclick events in my codes?
How can I pass the onclick events to the fragments below?
I saw some people suggested to use onInterceptTouchEvent(), but I am not sure if this applies to my case and I do not really understand how to use onInterceptTouchEvent().
Update:
I tried to override boolean dispatchTouchEvent(MotionEvent ev) to make the top fragment ignore the click event, however since the first touch event must be MotionEvent.ACTION_DOWN, it seems that there is no way for me to determine whether the touch event is click.
So, is there any other way to do this?
From the Documentation
The onInterceptTouchEvent() method is called whenever a touch event is detected on the surface of a ViewGroup, including on the surface of its children. If onInterceptTouchEvent() returns true, the MotionEvent is intercepted, meaning it will be not be passed on to the child, but rather to the onTouchEvent() method of the parent.
The onInterceptTouchEvent() method gives a parent the chance to see any touch event before its children do. If you return true from onInterceptTouchEvent(), the child view that was previously handling touch events receives an ACTION_CANCEL, and the events from that point forward are sent to the parent's onTouchEvent() method for the usual handling. onInterceptTouchEvent() can also return false and simply spy on events as they travel down the view hierarchy to their usual targets, which will handle the events with their own onTouchEvent().
So, you'll have to return true/false (in onInterceptTouchEvent method) according to your application flow logic. If you use OnTouchListener you can avoid using OnClickListener. See example in the documentation to understand it better.
This will give you the idea how onTouch and onClick are different.
You can use custom interfaces to pass data to lower fragment for manipulation

Analyzing touch events in a child view and let the parent handle them if condition not satisfied

I have extended a FrameLayout and overriden the onInterceptTouchEvent() and onTouchEvent(). This custom Framelayout will be the root layout of my ListView rows.
My requirement is that, if I tap on a row in the ListView, the onItemClickListener should be called, if I scroll (up/down) the list scrolls as usual. However if I scroll (left/right) or swipe (left/right), the custom FrameLayout should take some action based upon which row I perform the action on.
My idea was that, I would override onInterceptTouchEvent, monitor the events and if I see that ACTION_MOVE has been called enough to make it a scroll/swipe then I take control and do the task. If it's a tap or some other gesture then let the default flow occur.
The problem I face now is that if my framelayout does not contain any touchable child, then I receive no calls post ACTION_DOWN. And if I receive the ACTION_DOWN in my onTouchEvent() so as to analyze the kind of gesture, then I have no control left to let the default flow occur if I calculate and find that the action was actually just a tap/vertical scroll.
Is there some way in which a child can get hold of the events (ACTION_DOWN and ACTION_MOVE) and if a condition satisfies then perform an action or else let the parent handle these same events?
Is there some other approach that I can take to satisfy this requirement?

Issue with GestureListener

I am having a gesture listener attached to a view and I have onSingleTap Event handled.
It handles it properly if I tap anywhere in the view. But say if the View is having any subview and if I am tapping the subview the event does not get triggered.
Is there a way to pass this touch from child to parent? And also the children contains BUTTONS. so if the press is on a child button it should not pass the touch to parent. Otherwise it must pass it to parent. Anyway to achieve this?
You can override the Activity's dispatchTouchEvent(MotionEvent) in order to handle the dispatching yourself.
A good handling would be to first check if it triggers the gesture in your view and if not just call super.dispatchTouchEvent(MotionEvent);
The one thing you have to care about while doing this is to keep the gestures coherent with the rest of the platform.

Android capture all motion events and dispatch them to children

Alright, this is giving me headache for over an hour. I've got several ScrollViews in ViewFlipper. I want to implement swipe left/right gesture while letting the scrollview to scroll. The idea is quite simple - intercept the event and viewflipper, parse it and pass it to the children no matter what happened. This way I can easily detect the swipe without making mess with the events.
Theoretically all I have to do is to handle onTouch or something like that and return false, so the event will be dispatched to children. But the Android is so smart he won't send my any other events than ACTION_DOWN if I return false, because it thinks I don't want them.
So how can I capture all the motion events coming to the ViewFlipper and it's children and still dispatch them to the children? I can handle detecting the swipe myself.
Android is very nasty about this kind of things. You have to subclass and implement dispatchTouchEvent the way you need. Or you can grab some android code and modify it. I tried to use onInterceptTouchEvent() but found it unusable.
check out onInterceptTouchEvent() for a view group, if you wish to analyze the touches on view flipper and return false to pass the actions to the child views. look at the following link for more information.
http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)

Can parent and child views obtain the motion event simultaneously in Android?

In the main activity, there is a parent view and a child view.
I need to obtain the motion event in both views simultaneously. Is it possible to do that ?
Currently, I can only intercept the motion event in the child view. Parent view receives nothing.
The main task is both of them can get the motion events at the same time. Please help.
Thanks.
You can use these methods depending on what you are trying to do:
http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)
http://developer.android.com/reference/android/view/View.html#dispatchTouchEvent(android.view.MotionEvent)

Categories

Resources