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.
Related
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.
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.
I am trying to implement drag and drop on a GridView in android (ICS), but when I drag the item to the edge of the screen, the GridView doesn't scroll. How can I implement this functionality?
First of all if you don't need to support android 2.x ( API <11 ) this Andorid Drag and Drop tutorial is what you are looking for. I've never tried that way cause i had to implement it on 2.x but i took a look at it seems pretty straight forward.
To implement the autoscroll i guess you could use ACTION_DRAG_EXITED or ACTION_DRAG_LOCATION on the grid view and fire the scroll manually when you see that the location is next to the view bounds.
If you instead have to implement it on 2.x then it's going to be much more hard, you basically need to implement all the DRAG events, or a subset of them, by yourself.
I did in once with a ListView and what you have to do is:
Override the gridView touchEvent.
Use the actions ACTION_MOVE, ACTION_DOWN, ACTION_UP to fire the Drag events
Enable the drawing cache on the GridView items( setDrawingCacheEnabled ) and use getDrawingCache() to copy the bitmap of the view on DRAG_START.
Than what you have to do is to use that bitmap drawn on top of the GridView at the position that the ACTION_MOVE event gives you.
it's not easy but if you take your time you can manage to get it.
what more, I haven't check today but maybe you can try to google a bit an see if someone took the time to implement it and release it Open Source.
I need to implement HorizontalScrollView which is scrolled to predefined positions (similar to Home behaviour). It works with slow gestures, but does not work with neither flings nor arrow key press.
I hooked to View.onScrollChanged() and it is called when scrolling happened, but I can't determine when scrolling animation ends.
In theory there should be a way to say that fling movement is over. Is there such API?
I did not find a way to track scroll animation, but it's possible to move scroll logic from HorizontalScrollView class to derived by you, where you can control every aspect of scrolling
I'm replying to a post a year old but this might be useful for future readers.
For implementing HOME-Like scroll, use Gallery instead of HorizontalScrollView.
Using 'Gallery' you need not worry about stopping scrolling at predefined positions and it gives you a handler for Fling-gesture too.
Add your code in onFling() or onScroll() as per your requirement.
More info # http://developer.android.com/reference/android/widget/Gallery.html
The views are not cached in a ViewFlipper. Is there a way wherein we can get an image of the view and show it to user so that he sees the Ui as we see on Home scrren(when we swipe the previous view also moves along and when we lift our finger, only then the next view is shown completely.)
What I want to do is that when the user starts moving his finegr on screen, the view should also move along(create an image of view).
I am not getting to do this, as when we swipe the present view goes and next view comes, we do not get both visible when we r moving our finger on screen.
Please if anyone gets what I am trying to do, do help me.
Thanks,
Farha
It's tricky to get scroll and swipe tracking working on Android, while using ViewAnimator or its subclasses.
They allow you to set in and out animations and start them at a given moment, but they work with discrete, either-this-or-the-other-view animations. They are actually using FrameLayout and after in or out animation is executed, other views' visibility is set to View.GONE to hide them from showing up under/over your current View.
The Launcher and the Gallery application are actually doing the functionality you want, by using a different approach.
They track the user touch input (onTouchEvent()), on MotionEvent.ACTION_MOVE they perform animations manually and on MotionEvent.ACTION_UP snap to the appropriate view, just like in the iPhone.
Unfortunately, this approach is actually more complicated than it looks like.
With the manual handling, you have to ensure that you are taking care of everything related to the touch input. This includes a lot of flag-raising, value-checking, event-delegating, etc.
If you want to get better acquainted with this, take a look at these classes from Gallery3D or Launcher's source code.
One other way to get nice horizontal scrolling is to use HorizontalScrollView.
You have to figure out a way to recycle your views, like you would with a ListView and you have to add the snap-to-view logic, but if you have to take care of a small number of views it could be the easiest approach.
Hope that helps.