android – how to make my viewpager swipe from right to left-ThrowExceptions

Exception or error:

I have a viewpager and it contains some information but it lets me swipe the pages from left to right how can I make it swipe from left to right which means change its direction?

How to solve:

I think you can set current page to the last page using viewPager.setCurrentItem(). Then it will swipe left to right .. 🙂

###

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

There are two simple step which has 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.

###

You can swipe in both directions already. By default it shows page zero, but you could change that:

protected void onCreate(Bundle savedInstanceState) {
    // ...
    viewPager.setCurrentItem(myFragmentCount-1); 
    // ...

###

I’m sure it’s not relevant for the op, but I found a solution that worked for me (as modifying the data was a problem for my case).
I simply rotated the viewpager 180 degrees in the Y axis, and then rotated it’s subviews again in 180.

android.view.View

public void setRotationY(float rotationY)

Hope it helped anyone, as I think this is the most close to how Android mirrors all the os when change to rtl language.

###

You can do something like this,

ViewPager.setCurrentItem(lastposition);

###

First u need to implements OnPageChangeListener in your class
and second set the listener to your viewpager like
viewpager.setOnPageChangeListener(yourclass.this);
Hope This will work.

###

my solution. dynamically:

public class RTLViewPager extends ViewPager {

    public RTLViewPager(Context context) {
        super(context);
    }

    public RTLViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setAdapter(PagerAdapter adapter) {
        super.setAdapter(adapter);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if(getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL){
                setCurrentItem(adapter.getCount()-1);
            }
        }
    }
}

###

I know this question is old, but here is my version of things:

In order to get your viewpager to swipe RTL , regardless if it has fragments or views inside it you need to do the following things

step 1:
define an integer: R.integer.angle_rotation_for_rtl

for values it is 0

and for values-rtl it is 180

step 2:

viewPager.setRotationY(getResources().getInteger(R.integer.angle_rotation_for_rtl)); 

step 3:

   viewPager.setPageTransformer(false, new ViewPager.PageTransformer() {
            @Override
            public void transformPage(@NonNull View page, float position) {
                page.setRotationY(getResources().getInteger(R.integer.angle_rotation_for_rtl));

            }
        });

And the result – a smoothly working viewpager , no extra ifs

###

Don’t use rotation with ViewPager if you’re supporting API28 and above, because it makes ViewPager swiping too difficult on android pie, see this question setRotationY(180) on recyclerview or viewpager creating scroll issue in Android 9(API 28).
This Library helped me to implement RTL ViewPager https://github.com/duolingo/rtl-viewpager

Add it to dependencies and just make sure you add layoutDirection=”locale” to the RtlViewPager

<com.duolingo.open.rtlviewpager.RtlViewPager
        android:layoutDirection="locale"
        android:keepScreenOn="true"
        android:id="@+id/quranViewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true"
     />

Update

ViewPager2 is locale friendly so it’s automatically will change swipe direction according to the current device locale.

Some helpful links on the implementation of ViewPager2:

Android docs

Sample

###

For letting your horizontal ViewPager behave like RecyclerView when LayoutDirection change just add this method to your custom ViewPager;

public class LoopViewPager extends ViewPager {
....
...
    public void setLayoutDirection(int layoutDirection) {
        if(layoutDirection == ViewCompat.LAYOUT_DIRECTION_LTR) {
            setRotationY(0);
        }
        else if(layoutDirection == ViewCompat.LAYOUT_DIRECTION_RTL) {
            setRotationY(180);
    }
}

and where you use your viewPager set it dynamically like this;

viewPager.setLayoutDirection(context.getResources().getConfiguration().getLayoutDirection());

###

I totally understand that the question is answered perfectly; but if you’re using RTL language; for instance if you have a book that you want to show in ViewPager; then besides the answer, you might also consider to flip the pages over to get the right page; to do that modify your ViewPager adapter’s getItem() method

@Override
public Fragment getItem(int position) {
    return SomeFragment.newInstance(N_PAGES - position - 1); // to filp the pages
}

###

in layout:

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layoutDirection="rtl">
        <androidx.viewpager.widget.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layoutDirection="locale"/>
    </LinearLayout>

in code:

  //region swipe right to left
        pager.setRotationY(180);
        pager.setPageTransformer(false, new ViewPager.PageTransformer() {
            @Override
            public void transformPage(@NonNull View page, float position) {
                page.setRotationY(180);

            }
        });
  //endregion

###

You don’t any third party library. Android recently added new UI component called ViewPager2 which supports RTL View Pager.

https://developer.android.com/jetpack/androidx/releases/viewpager2

Please find below link to an answer. The answer talks about Migration guide and sample github app to support RTL view pager.

https://stackoverflow.com/a/59558911/651377

Leave a Reply

Your email address will not be published. Required fields are marked *