22

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?

User
  • 509
  • 2
  • 8
  • 23

14 Answers14

23

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

Ashish Kumar
  • 960
  • 2
  • 11
  • 20
17

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.

Aman Gupta - ΔMΔN
  • 2,971
  • 2
  • 19
  • 39
9

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); 
    // ...
andy256
  • 2,821
  • 2
  • 13
  • 19
  • thanks that's what I did when Anshish kumar answered me will mark it as usefull.hope that this will get you some points. – User Sep 13 '13 at 08:53
  • I prefer to set smooth scrolling to `false` to transit immediately by calling `viewPager.setCurrentItem(myFragmentCount-1, false)`. – Hafez Divandari Jan 31 '16 at 04:06
5

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. 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

Abed
  • 3,999
  • 1
  • 17
  • 28
4

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.

Nadav96
  • 1,274
  • 1
  • 17
  • 30
3

You can do something like this,

ViewPager.setCurrentItem(lastposition);
Basim Sherif
  • 5,384
  • 7
  • 48
  • 90
3

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);
            }
        }
    }
}
itzhar
  • 12,743
  • 6
  • 56
  • 63
3

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

Lena Bru
  • 13,521
  • 11
  • 61
  • 126
2

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.

Nand
  • 614
  • 4
  • 10
1

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
M.Namjo
  • 374
  • 2
  • 13
0

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());
OMArikan
  • 306
  • 1
  • 3
  • 9
0

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
}
Zain
  • 37,492
  • 7
  • 60
  • 84
0

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

Alok Gupta
  • 1,806
  • 22
  • 21
0

If You want to move left to right -> viewPager.setCurrentItem(viewPager.currentItem+1,true)//pass Boolean for smooth scrolling If You want to move right to left -> viewPager.setCurrentItem(viewPager.currentItem-1)//without smooth scrolling

ShehryarAhmed
  • 95
  • 1
  • 11