How to Implement PageTransformer with Swipeable Tabs

6.5k views Asked by At

In my example code i have three Swipeable Tabs in MainActivity.java namely : Android, IOS and WINDOWS and i am using swipe to switch between Tabs.

Now, i have to implement PageTransformer with Swipeable Tabs, so here i need your help, is it possible, if yes so how ?

MainActivity.java:-

    public class MainActivity extends FragmentActivity {
    ViewPager Tab;
    TabPagerAdapter TabAdapter;
    ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TabAdapter = new TabPagerAdapter(getSupportFragmentManager());

        Tab = (ViewPager)findViewById(R.id.pager);
        Tab.setOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {

                        actionBar = getActionBar();
                        actionBar.setSelectedNavigationItem(position);                    
                    }
                });
        Tab.setAdapter(TabAdapter);

        ..............
    }
}

Here is what i want to implement in my program :

enter image description here

and this is how my Tab looks :

enter image description here

3

There are 3 answers

0
Sun On BEST ANSWER

I found my solution here, its really easy to implement

Usage is pretty straightforward, just attach a PageTransformer to the ViewPager:

viewpager.setPageTransformer(false, new ViewPager.PageTransformer() {
    @Override
    public void transformPage(View page, float position) {
        // do transformation here
        }
});

rotates the pages around their Z axis by 30 degrees; you don’t need to normalize for this one. The effect is similar to the cover flow UI pattern:

@Override
public void transformPage(View page, float position) {
    page.setRotationY(position * -30);
}

so here is my final code, which i used:

viewPager = (ViewPager)findViewById(R.id.pager);

        viewPager.setOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {                       
                        actionBar = getActionBar();
                        actionBar.setSelectedNavigationItem(position);                    
                    }
                });

        viewPager.setAdapter(tabAdapter);        
        viewPager.setPageTransformer(false, new PageTransformer() {

            public void transformPage(View page, float position) {

                page.setRotationY(position * -30); // animation style... change as you want..
            }
        });
0
Squonk On

Create an inner class of your Activity...

public class MyPageTransformer implements ViewPager.PageTransformer {

    public void transformPage(View view, float position) {
        // Do your transform here
    }
}

Then in your Activity you can set it on your ViewPager. Example...

Tab.setPageTransformer(true, new MyPageTransformer());

Read more at Customize the Animation with PageTransformer There are even a couple of examples of transforms to try out and to customize yourself.

0
Pragnesh Ghoda  シ On

You have to use setPageTransformer() to do this type of navigation.. use below code to your pager..

Add This Code To Your MainActivity.java

Tab = (ViewPager)findViewById(R.id.pager); // you have defined view pager as `TAB`

    Tab.setPageTransformer(false, new PageTransformer() {

            public void transformPage(View page, float position) {

                page.setRotationY(position * -30); // animation style... change as you want..
            }
        });
        Tab.setCurrentItem(pos);
    }

Get More Animation Styles From Here.. Customize the Animation with PageTransformer or ViewPager transitions