Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing viewPager swipe direction

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.

like image 772
Eliran Avatar asked Sep 07 '15 17:09

Eliran


People also ask

How do I stop ViewPager swiping?

To enable / disable the swiping, just overide two methods: onTouchEvent and onInterceptTouchEvent . Both will return "false" if the paging was disabled. You just need to call the setPagingEnabled method with false and users won't be able to swipe to paginate.

How do I use RTL ViewPager on Android?

So It is the very simple technique to change the swipe direction of the Viewpager. In order to support RTL in your app, you first need to add to android:supportsRtl=”true” the element <application> in your manifest file.

Is ViewPager deprecated?

This function is deprecated.


1 Answers

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.

like image 150
Aman Gupta - ΔMΔN Avatar answered Sep 30 '22 05:09

Aman Gupta - ΔMΔN