Changing viewPager swipe direction

Eliran picture Eliran · Sep 7, 2015 · Viewed 12.1k times · Source

I'm using TabLayout and viewPager with an option to swipe the viewPager to the left or right in order to navigate between pages.

My problem is, that my application is RTL based (right-to-left) and the swipe direction is reversed.

I'm trying to change the swipe direction from the default to the reversed version. I've been searching alot on the web and couldn't find a way how to do it.

I'm using android.support.v4.view.ViewPager;

and this is how I initialize my TabLayout with the viewPager:

// View Page Adapter
        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter
                (getSupportFragmentManager(), tabLayout.getTabCount());
        //View Page Adapter Configuration
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        viewPager.setOffscreenPageLimit(3);
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

Summary: Currently, When I'm swiping the viewPager to the left, it shows the next page. in RTL, when you swipe the viewPager to the right, it shows the next page.

It might be hard to understand, but here it is. Swiping right shows the next page

Swiping right shows the next page

while I need swiping right to show the previous page.

Answer

Aman Gupta -  ΔMΔN picture Aman Gupta - ΔMΔN · Jan 23, 2017

It is the very simple techniqe to change the swipe direction of the Viewpager.

There are two simple step which have to follow you,

1. Change the rotation of the View pager,

viewpager.setRotationY(180);

2. Then again change the direction of the fragment container which is the child of viewpager,

recyclerView.setRotationY(180);

Note: In my case I have used recyclerview as the child of the view pager.

This is 100% worked for me.