Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an Android view that flips between views on swipe/fling

I'd like to make a view in my Android app that flips between multiple views on a swipe/fling. I'd like it to behave more or less like the Android Launcher behaves when flipping between views. In particular,

  1. It should flip views on swipe.
  2. Generally a swipe will flip between one view and the next. It should not fling across all of the views.
  3. If you swipe slowly, you should see the views dragging as you're swiping, eg. the way the Launcher does it.

I tried using a ViewFlipper with a GestureOverlayView as per Romain Guy's blog post here, but there's no indicator to the user as they're swiping. This makes discoverability difficult, which is presumably why Launcher does it the way they do.

I tried using a Gallery object, but when I swipe from left to right, there's a certain amount of momentum that flings the users through all the views rather than just taking them to the next view.

Is there a good way to accomplish what I'm trying to do?

like image 250
emmby Avatar asked Feb 21 '11 21:02

emmby


3 Answers

I know this is an old question but ViewPager is created for this exact same purpose. ViewPager is part of android compatibility package and more can be found at http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html

like image 85
binW Avatar answered Oct 19 '22 15:10

binW


Take a look at HorizontalPager. It's actually based on RealViewSwitcher, which in turn is based on the Android homescreen's code, and supports snap-to paging with drag feedback, as well as nested vertically-scrolling subviews. Gesture support for fast swipes isn't all it should be, but this may get you part of the way there (and I'd welcome contributions back).

EDIT: As of 2012 you're much better off using Google's ViewPager - it's in the compat library.

like image 14
Yoni Samlan Avatar answered Oct 19 '22 15:10

Yoni Samlan


Check out SwipeView within this project https://github.com/fry15/uk.co.jasonfry.android.tools It does exactly what you want it to do and is super simple to implement.

like image 6
Jeff Gilfelt Avatar answered Oct 19 '22 17:10

Jeff Gilfelt