Sunday, April 8, 2012

Is it possible to do this Android screen animation with a ViewPager?


I need to make 2 screens with custom animation like explained below :



Screen 1 Screen 2
----------------------------- ------------------------------
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| List 1 | List2 | ---------> | List 3 | List 4 |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
----------------------------- ------------------------------


  • User makes a long touch on an item in List 1 and slides from left to right.

  • The view containing List 1 moves from left to right (till the end of the screen) and fades. Screen 2 is shown.



Is it possible to do this animation using ViewPager ? If yes, How?



I would like to do this via ViewPager because I'm using Fragment s pretty extensively and I have implemented many screens as fragments already.



If anyone needs clarification about the animation or the UI, please let me know.



Update : I was able to implement both of the screens in a single activity which I have partially explained here . I can implement the same in a single fragment. But being able to implement as different Fragment s in a ViewPager would still help.


Source: Tips4all

2 comments:

  1. ViewPager, too me, seems like overkill. Unless you want to add more screens later, or some other requirement. For these simple screens you can do it with ActivityAnimations. If you put Screen1 and Screen2 in a seperate Activity, you can animate the Activities using simple styles. You don't need to code, simply define the Enter and Exit styles for your activities, and they will be executed.

    So, unless you have another reason for using the ViewPager, you can accomplish the same effect by the following (not tested):

    Android Manifest.xml

    <activity android:name=".Screen1" android:theme="@style/Animated"></activity>
    <activity android:name=".Screen2"></activity>


    Your themes.xml

    <resources>
    <style name="Animated">
    <item name="android:windowAnimationStyle">@style/Animation.ScreenAnimation</item>
    </style>
    </resources>


    Finally, in your styles.xml

    <style name="Animation"></style>
    <style name="Animation.ScreenAnimation" parent="android:style/Animation.Activity">
    <item name="android:activityOpenEnterAnimation">@anim/slide_in_right</item>
    <item name="android:activityOpenExitAnimation">@anim/slide_out_left</item>
    <item name="android:activityCloseEnterAnimation">@anim/slide_out_left</item>
    <item name="android:activityCloseExitAnimation">@anim/slide_in_right</item>
    </style>

    ReplyDelete
  2. I have successfully used vertical ListViews inside of ViewPagers before. How about trying a horizontal scrolling list view inside of your ViewPager?

    ReplyDelete