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.
Related
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
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?
The problem is the the scrolling will intercept with touch event has set to the parent layout.
Can I keep the onTouch event with the scroll in ScrollView ?
This is a very tricky part. There is an overriden method from Activity which is: public boolean onTouchEvent(MotionEvent event)
This is the general method that interprets all the touch events from the whole screen. And you could say, "ok, I can implement this and I am good to go..". And here comes the difficult part on how android works.
As you know every View has its own onTouchEvent() method that you could implement in order to add some custom implementation. So which method will listen? The ScrollView or the Activity? It appears that these touch events go from the "inside" elements to the "outside" elements. I mean parent-child relations.
Another thing to take into account is that the onTouchEvent method returns a boolean. This boolean parameter determines whether the touch event should go one level up or it is handled by the current View. Meaning that if you have a CustomViewA that implements the onTouchEvent() and CustomViewB implementing its own touch event, and the A is a child in B then the touch event would go through A first and if it is not handled it would go to B.
So basically yes it could be done. It depends on what touch event you wanted to do.
So in our case, the ScrollView returns true when the touch events are a horizontal. The activity's touch event will be handled only if the ScrollView touch event is not handled by itself then you are fine. Otherwise you have to override and implement the on touch event of scroll view and in some cases you have to return false so as for the whole layout to implement it. Good luck with the last part. I started to implement a fling effect but came up with some difficulties so I have implemented a 2 finger move with scroll view in it and it works like a charm.
This is about a week of research and experimenting and it is an overview of what I came up with. if you find anything else please let me know. Hope it helped.
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?