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)
Related
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?
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.
In my app, I have to handle the client side session management regarding UI that means when the user never touches on the screen for a while(eg: 1min), he will be prompted to login screen(in my case). for this I planned like this: added ontouch listener to the root view of every layout and placed my session logic. But the problem is, every layout contains lot of textviews,listviews,buttons,etc. I dont want to add ontouch listener to all these views, for this I added to only root view of my layout. when i touch on textviews, the root view's ontouch listener is getting that touch event and working fine. but when i touch on the remaining views, the root view's ontouch listener is not getting that touch event. Can someone tell me whether my approach is correct or not? If it is correct then how can I do for the remaining views?
thanks,venu
I don't know, if you're doing it right, but to get notification for all touch events in a parent view you can override the onInterceptTouchEvent(MotionEvent ev) method of the ViewGroup class.
For details check:
http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)
Regards:
Ripityom
Sorry for the complex question let me explain.
I've created a custom widget that handles some ontouch events. What i want to do is when i start a touch event on that custom widget (onDown) i want that widget to keep handling these event even if the absolute coordinations are not in that widget.
I have a scrollview and on top of that(inside the scrollview) a widget that handles (left - right) scrolls. But if i move the finger vertically the ontouch events are consumed and handled by the scollview. If there a way to forbit scrollview to handle touch events , or better force the custom widget to keep handling the touchEvent, if i start the ontouchEvent inside the custom widget?
UPDATE I came across that http://groups.google.com/group/android-framework/browse_thread/thread/2cdd4269dfb2772e?pli=1 .
That works in my case if the custom widget is NOT inside a scrolling view like "ScrollView". Trying to solve the "Beeing inside a scrolling object. How to not send these on touchevents to the parent? Returning true doesn't solve the problem
In your widget's onTouch handler (e.g. during ACTION_MOVE), call the parent.requestDisallowInterceptTouchEvent(true). That method basically is asking the parent (in your case the scrollview) and its ancestor not to intercept the touch event.
You will need to implement a special subclass of ScrollView, which allows your widget to tell it to disable scrolling when desired (so it won't start scrolling after enough movement and take events from your widget). The custom ScrollView can override this method:
http://developer.android.com/reference/android/widget/ScrollView.html#onInterceptTouchEvent(android.view.MotionEvent)
When you don't want the base class to consume touch events for scrolls, return false here instead of calling the base implementation.
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?