开发者

Android: Next/Previous fling that sticks to the finger similar to Home Screen

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 overri开发者_高级运维de 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.


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


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


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);


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜