Letting Gestures fall through to different Views on Android - android

I've got a custom View which wills the screen. I've built its onDraw code to support a pinch-zoom style of zooming in and out. The View itself handles all of this work.
Additionally though, I'd like to allow the user to swipe across the screen, either left or right, so that I can swap out that view with the view before or after (similar to a paging situation).
I was able to capture and respond to the swipe left and right using a GestureOverlayView, but now that overlay view is intercepting my pinch-zooming touch events. How can I allow those pinch-zooming touch events to fall through to the custom View will grabbing the swiping gestures up at the activity level?

I think you might want to use a ViewFlipper with a GestureDetector instead of a GestureOverlayView.
http://android-er.blogspot.com/2012/02/implement-swiping-page-effect-using.html
with a setup like this, the gesture detector should only accept left/right swipes, while the view currently displayed still controls the pinch-zoom effect

Related

How do I cause vertical two-finger swipe behavior to cause a horizontal slider to move left or right?

I have a horizontal slider in my app, and I want to make it so that if you have the slider selected, you can do the two-fingered swipe up or down gesture from anywhere on the screen (The scroll up or down gesture) to move the slider left or right. I haven't been able to find anything through google about how to change vertical swipe behavior for Talkback and was wondering if there was in fact a way to change this.
I'd really suggest not doing this, it isn't how Android works so it will confuse your users, and be a big source of bugs as any behavior like this can cause touch errors on completely separate views. I just fixed a problem on something similar in my company's app.
But if you really want to do this- your root ViewGroup needs to intercept touch events if there are two fingers down and it moves far enough to qualify. Read https://developer.android.com/training/gestures/viewgroup#intercept for an explanation of how a parent view group can steal touch events from the child. This is the same way ScrollView works to scroll its contents. In fact, looking up the AOSP implementation of ScrollView would give you good example code.

Android nested touch handling

I have a ViewPager inside a custom pager. Both swipe in horizontal direction. As I understood the Android touch model, my custom pager should monitor touch events going through onInterceptTouchEvent, checking for a swipe gesture and intercepting when one is detected, but also respect requestDisallowInterceptTouchEvent so that if a child view should consume the same horizontal swipe gesture my custom view is monitoring for it is allowed to.
So far so good, my custom pager handles horizontal swipes well for non-scrolling or vertically scrolling child views and for horizontally scrolling child views, respects the disallow request so the child view can use the horizontal gesture.
However, if the horizontally scrolling child view has reached the end of its content so it cannot scroll any more, I would expect it to rescind its disallow request, so that the parent can intercept and consume the motion, but reading through the source (https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v4/java/android/support/v4/view/ViewPager.java, https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/ScrollView.java), I cannot see it ever calling requestDisallowInterceptTouchEvent with false argument, meaning once it has identified a gesture as a swipe, it will consume it regardless of whether it can actually make use of it or not.
Is my understanding of Android's touch handling system flawed, or are the standard view classes' touch handling too crude in this regard?
This is a question to improve my understanding of Android's touch handling model so I can work better with the system, not a request for workaround suggestions (though if the answer is authoritatively "the system does not allow for this", good workarounds are probably welcome to those who have a similar problem and find this question while searching)
Have you looked into ViewGroup.onNestedFling()?
The pertinent excerpt from its documentation:
If a nested scrolling child view would normally fling but it is at the edge of its own content, it can use this method to delegate the fling to its nested scrolling parent instead.

How android swipe event reflected in View?

Such as touch the screen and move or use "input swipe" in adb shell, how android system decide to scroll or not and the distance to scroll?
Simple answer. Gesture detection. Scrolling views like ScrollView, ListView and RecyclerView will register to receive onTouchEvent events from the screen. They will then track those events over time to detect things like swipes, including the direction, velocity and acceleration of the swipe.
From there, they can make a decision on whether to scroll, how far to scroll, and in what direction. Each view will do this differently based on how their content is laid out, whether there is additional content that be scrolled to, etc..
In particular ScrollView uses a VelocityTracker to track the series of touch events, and uses that to calculate what to do. There's other options (like the GestureDetector class) available to do simple gesture detection in custom views.
Do yourself a favor, and look at the source code for ScrollView in your IDE.
In particular the onTouchEvent and onInterceptTouchEvent methods.

Android move items with acceleration

I cant seem to find the answer to this, mainly because I don't know what it is called.
I am going to expand a few features in my app, currently users can touch and drag to move forward in a list of images. What I want is for the users to "swipe" there finger and then all of these images will move under acceleration and will slowly come to a stop.
Is this a gesture? If so is it the "Fling" gesture?
There are several ways to do so.
Use ListView
Use Gallery
Use ScrollView
Use HorizontalScrollView
Write your custom ViewGroup or View
For the last approach, you have to detect the Fling gesture as you said and handle all the scrolling animations involved.

Gesture problems with a ViewFlipper that contains a ListView

I have a ViewFlipper where one of the views is a ListView. To move back and forth between the views, I have a GestureListener that detects left and right swipes. Sometimes the left & right swipes interfere with the ListView. That is, when I want to switch to the next view by swiping left/right, I may accidentally click on an item in my list.
Is there a good way to prevent this interference?
Have a look at http://android-journey.blogspot.com/2010/01/android-gestures.html.
The SimpleGestureListener from this page is a great solution to gesture detection. When run in dynamic mode (the default), it intercepts touch events that are determined to be gestures to prevent them from performing other actions. Other touch events are not interfered with.
If you are only interested in swipe gestures I recommend disabling the code for detecting tapping and only listening for swipes.
If you want something a little snazzier than a ViewFlipper (something more like the Android home screen) try out this new addition to the Android compatibility libs: http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html?m=1

Categories

Resources