I have a custom linearlayout in which I have two gridviews.
I want to have multitouch on the complete linearlayout, ie, first touch can be in one gridview and second touch can be in another gridview and the linear layout should be able to get multitouch events for this (ACTION_DOWN and ACTION_POINTER_DOWN). I am able to get this by overriding onInterceptTouchEvent in my custom layout.
Now I also want the gridview to scroll independently if required. Although gridview is a scrollable view, it is not able to do so, because I have intercepted the 'down' event. I see that when I try to scroll two events are generated : 'ACTION_DOWN' and 'ACTION_MOVE'.
Since I need ACTION_DOWN for multitouch use case I want it to be sent to linearlayout as well.
Is there a possibility to send the ACTION_DOWN event to both the linearlayout as well as my child view which is gridView in this case?
Need help.
You don't have to intercept ACTION_DOWN just to monitor that it flowed through your container view. In other words, you can override onInterceptTouchEvent() to monitor the touch events before they are passed to the appropriate child view by the framework, but as long as you don't screw with the return value it won't actually steal subsequent touch events from the grid elements. For example:
public boolean onInterceptTouchEvent (MotionEvent ev)
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
//Touchdown!
}
//Don't modify this and touch interaction will happen normally
return super.onInterceptTouchEvent(ev);
}
This allows you to see the event without taking it away from the child element(s).
Related
Saying that I have the following view:
<FrameLayout>
<View />
</FrameLayout>
There are something that I need to achieve:
Detect move event on FrameLayout, which is MotionEvent.ACTION_MOVE. This can be achieved easily by implement OnTouchEvent. But the problem is coming from the child View
I would like to implement onClick event on the child View, therefore I have to consume the event in the OnTouchEvent of the child View. The problem is when I return true (consumed) on the child View. The FrameLayout (parent view) won't be able to receive any further events (only MotionEvent.ACTION_DOWN can be received)
So the question is how can I able to dispatch further events to parent view? In Android, view like ScrollView are able to achieve that. How can we achieve same thing as ScrollView? (Still able to dispatch event further down but still able to capture the gesture to scroll)
This is just an example. The real code have multiple nested view inside
My fragment view implements a touch listener to listen to various gestures but my horizontally fashioned recyclerview consumes any gesture performed over it.
Basically I want to listen to vertical swipe when performed over recyclerview but I never receive callback in fragment view's gesture detector.
I was able to get a workaround for it. I had to subclass ViewGroup and override onInterceptTouchEvent and handle things using MotionEvent.{action_type}. Then I replaced the fragment view's parent view group with this custom view group inside xml layout.
So in nutshell, if I detect it as a vertical swipe I return true in my onInterceptTouchEvent otherwise false. If its false, the touch event is passed onto the child views for handling. Works perfect.
I know there are several answers which describe how to lock a ViewPager.
But I want that my ViewPager is able to scroll only if the touch doesn't occour on a specific child.
So how can I "filter" out which child views are touched?
example code of how I want to lock my ViewPager:
for (Integer resource:mResources) {
if(resource.intValue()==view.getId()){
touchTriggeredOnView(view.getId());
}
}
The reason why I want to do this is, that I have a custom view which needs to detect fling gestures.
This custom view is a child of a Fragment and the Fragment is a child of the ViewPager.
So: ViewPager->Fragment->CustomView.
I'm able to detect the DOWN but not the FLING event inside my CustomView.
I think that the ViewPager captures the FLING event --> bad thing.
I hope that my question is clear enough.
onTouch and gesture listeners in Android have boolean return values which indicate whether event (touch, fling, click) is consumed or not.
ViewPager probably consumes "Fling" event, however you could override onTouch, gesture listener in ViewPager and return false - thus not consuming touch event and passing it to your custom view.
I want to use a ListView (and have done this successfully before) containing custom Views.
Basically these custom views are vertical sliders, obviously conflicting with the natural behaviour of the ListView.
I fill it with my custom Views from an Adapter, and these react to the touches on the Items,
but once I move my finger more than a few pixels, it will scroll, and the custom View will not receive any touch-events anymore.
How can I (nicely) prevent the ListView from scrolling, when I touch my own components?
Can I somehow disable ListView Selection, and just forward the touches to the Items, but still use the scrolling behaviour?
Thank you in advance.
To prevent the scrolling of listview you can inplement on touch listener as follows
listView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_MOVE)
{
return true;
}
return false;
}
});
hope it will work and if it us useful to you give vote
You can intercept all touch events using onInterceptTouchEvent() in your root layout (one that contains the ListView, like a FrameLayout) as found here.
What you do there is capture the motion events (return true when a MotionEvent.ACTION_DOWN comes in), capture the following event in onTouchEvent(), decide whether the motion is meant for the list items or the list itself and accordingly dispatch the events.
Don't expect this to work easily. Understanding the flow of motion events and the interaction between onInterceptTouchEvent() and onTouchEvent() is challenging and making it work even more so. But I'm confident that this is a feasible way to solve your problem.
I tried to understand how Android handle touch event and got a little bit confused. From what I understand touch event are send to the root view and pass down to the children.
I have a FrameLayout that is a container for Fragment.
First fragment view is a ScrollView, second one is some kind of Gallery (HorizontalListView) and the last one is also FrameLayout. Only one fragment in the layout each time.
What I want to do is to identify user swipes on the screen, for the app use. I want to count the swipes and do something after some number of swipes.
I tried to put a OnTouchListener on the top FrameLayout but it doesn't get called when the child is the ScrollView or the Gallery. I tried to return false and also true in the end of onTouch, but I get same result - it's never being called.
How can I do it?
I just want to "transparently" handle the touch events and passing them on like I didn't even touch them.
My understanding is that it actually goes the other direction. The Child views get their event triggered first (sort of). The root view get's it's dispatchTouchEvent() called, which propagates the event down to the children's onTouchEvent(), and then, depending on whether they return true or false, the parent's onTouchEvent() is called.
The normal solution for intercepting things like this is to override dispatchTouchEvent(MotionEvent ev) in one's activity like so:
#Override
public boolean dispatchTouchEvent (MotionEvent ev) {
// Do your calcluations
return super.dispatchTouchEvent(ev);
}
The documentation for this one is here. Note that you can also override that method in any ViewGroup (such as a FrameLayout, etc)