3

I want to give my app a nice touch by allowing users to slide the page left or right instead of just using next/previous buttons (similar to the home screen).

What is the best way to do that? I assume I would have to override one of the Activity.on... methods and that I would also have to put my page's main View in a ViewGroup that allows me to shift pages left and right.

700 Software
  • 85,281
  • 83
  • 234
  • 341

5 Answers5

3

ViewFlipper is your friend!

Here you can see a nice video of the ViewFlipper in action and also a very good tutorial:

http://www.inter-fuser.com/2009/07/android-transistions-slide-in-and-slide.html

RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
  • Thank you. Now I just have to find out how to implement "fling". – 700 Software Jan 20 '11 at 15:10
  • 1
    @George Bailey: I registered a touchlistener on the Viewflipper. Every time a touch down event was received a stored time and position. On touch up event I compared time and position to the touch down event. If enough time was passed and it was obvious that the finger has moved left respectively right, I called the flipper to move left or right accordingly. – RoflcoptrException Jan 20 '11 at 15:13
  • Is it possible for me to see touchmoved events so I can make the views stick to the finger until you let go and then decide whether to return to original or shift to next? (similar to the home screen) – 700 Software Jan 20 '11 at 15:17
  • 1
    Not sure if I understand you correctly. But you can override onTouch (http://developer.android.com/reference/android/view/View.OnTouchListener.html#onTouch%28android.view.View,%20android.view.MotionEvent%29) in your activity – RoflcoptrException Jan 20 '11 at 15:32
  • I am working on that now. Using `FrameLayout`. I found `onTouch` here http://stackoverflow.com/questions/4324362/android-detect-touch-press-vs-long-press-vs-movement and I found `FrameLayout` as a suggested replacement for `AbsoluteLayout` which is deprecated – 700 Software Jan 20 '11 at 15:49
  • Hmm, FrameLayout may not work. I don't see where I can set the top and left for the child views. – 700 Software Jan 20 '11 at 15:52
3

The solution is even easier these days with the release of Compatibility Package r3. You can download here: http://developer.android.com/sdk/compatibility-library.html

It includes

  • ViewPager: A ViewGroup that manages the layout for the child views, which the user can swipe between.
  • PagerAdapter: An adapter that populates the ViewPager with the views that represent each page.

and Fragment versions of those, if you are that way inclined.

The pager code is compatible back to API version 4 (1.6), and I just implemented a dynamically generated viewPager coming off a dynamically generated ListView in about 2 hours. I'm a novice, so this is definitely the preferred path.

There is an example app here: http://geekyouup.blogspot.com/2011/07/viewpager-example-from-paug.html

jaseelder
  • 3,403
  • 3
  • 25
  • 26
0

If one wants to flip between two activities perhaps one can apply this animated transition:

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
finish();
//transition using XML view animations
overridePendingTransition(R.anim.slideinfromright, R.anim.slideouttoleft);
Lumis
  • 21,517
  • 8
  • 63
  • 67
  • My situation would make flipping activities impractical. I have all I need except I don't know how to detect when the finger moves. I only have instructions to detect when it touches and when it stops touching. – 700 Software Jan 20 '11 at 15:30
  • 1
    Check if this can help you about android guesture detection: http://android-journey.blogspot.com/2010/01/android-gestures.html – Lumis Jan 20 '11 at 15:34
  • Looks like `onScroll` is helpful, now I just have to find how to set the absolute coordinates of a child view. – 700 Software Jan 20 '11 at 15:59
  • I think that is possible with a custom view check my long post which is using onSizeChanged. It is at that point that you should be able to get the cooridinates. http://stackoverflow.com/questions/3779173/determining-the-size-of-an-android-view-at-runtime/4634272#4634272 – Lumis Jan 20 '11 at 16:31
  • Once when the view is drawn on the screen one can get its size and location, but that is not known yet in Activity onCreate(). – Lumis Jan 20 '11 at 16:38
  • Already found a working solution: http://stackoverflow.com/questions/4142090/how-do-you-to-retrieve-dimensions-of-a-view-getheight-and-getwidth-always-r/4406090#4406090. I am about to test `onScroll` – 700 Software Jan 21 '11 at 01:18
0

Use GestureDetector to detect if the touch event is a scroll.

If the first event to the first call to onScroll is ACTION_DOWN then you should see if it was a dominantly horizontal scroll. If so then your scroll is started and you should shift the absolute position of the view that fills the page.

For non deprecated absolute positioning, see my answer here Android: Alternative to AbsoluteLayout (I really do need absolute positioning)

You will want to be cautions of whether you return true to consume the touch events or not.

GestureDetector does not have a callback for scrolling having stopped. You will have to check if there was an ACTION_UP before you call GestureDetector.onTouchEvent and if there was an action up and you did have an unfinished scroll then you should set the absolute position to the destination location and use a TranslateAnimation to make it look nice moving from current to destination.

Edit:

GestureDetector did not work well at all if the child views also wanted to respond to touch events. I ended up creating a subclass of FrameLayout (one of the most basic layouts and the closest thing to a non intrusive parent view) and overriding dispatchTouchEvent. I just took all the events and did the detection myself.

Community
  • 1
  • 1
700 Software
  • 85,281
  • 83
  • 234
  • 341
0

cant we use Gallery view here?? with the adapter the whole page can be inflated inside gallery view adapter getView() and it will manage the left right scrolling perfectly.

dcool
  • 4,039
  • 3
  • 16
  • 14