how to make my viewpager swipe from right to left

前端 未结 13 625
一整个雨季
一整个雨季 2020-12-05 14:42

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 direc

相关标签:
13条回答
  • 2020-12-05 15:12

    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

    0 讨论(0)
  • 2020-12-05 15:14

    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
    
    0 讨论(0)
  • 2020-12-05 15:15

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

    0 讨论(0)
  • 2020-12-05 15:15

    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

    0 讨论(0)
  • 2020-12-05 15:19

    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
    }
    
    0 讨论(0)
  • 2020-12-05 15:22

    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);
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题