How to Implement PageTransformer with Swipeable Tabs

折月煮酒 提交于 2019-12-06 04:06:17

问题


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 :

and this is how my Tab looks :


回答1:


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




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/25136557/how-to-implement-pagetransformer-with-swipeable-tabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!