Android: Slide gesture and animation for switching

2020-05-29 12:01发布

问题:

I'm sure this has been answered somewhere, already, but I just can't find it: I have a TabHost with three tabs, each containing different views (not Activities). What I want is to make a swiping gesture from right to left and let the current view slide out to the left and the next view slide in from the right, thus changing the current tab. So, I want the switch between tabs to be triggered by a gesture (rather than only by clicking on the tab) and also want to show an animation when the current tab is changed.

I looked at the ViewFlipper examples and tutorials, but I can't find one that explains how to do this to slide between tabs.

Again, sorry if this has been answered before, I just can't find it.

回答1:

Basic Gesture Detection - stackoverflow

Introduction to Gestures - mobile tuts

you have to definde your gestures with the android gesture tool (you can find it in the emulator) and implement a gesture listener to listen for gesture events.



回答2:

You can now find support for this behavior out-of-the-box in the Android SDK compatibility libs. It won't be using a TabHost, but it supports the behavior you describe: ViewPager



回答3:

Two things has to be done:

  • Register Guesture listening object to every view group
  • On detecting guesture event, create an animation object to move this view group and at end, remove this view group

Please see the answer by Blundell in this post: How to Animate Addition or Removal of Android ListView Rows