Skip to content Skip to sidebar Skip to footer

How To Reverse The Direction Of Viewpager To Left To Right Order?

I have viewPager with several views. the default behavior of viewPager is that the first item is displayed first, then swiping right to left displays the second view right to the c

Solution 1:

why don't you reverse the list of you view in view pager and use setCurrentItem(int) or setCurrentItem(int,boolean) and set the last one at the launch of the activity/fragment..

Solution 2:

You can simply use setCurrentItem() to navigate to the last page in the ViewPager when it's attached to the View.

Solution 3:

The solution for ViewPager2

I was looking for a way to reverse viewpager2 and came up with this one. It's a dirty hack, but it works

The idea is to mirror the viewpager horizontally, and then mirror each page back

var ViewPager2.isReversedDirection: Booleanget() = scaleX == -1fset(value) {
    if (value) {
        scaleX = -1f//  mirror viewpagerval listener = object : RecyclerView.OnChildAttachStateChangeListener {
            overridefunonChildViewDetachedFromWindow(view: View) = UnitoverridefunonChildViewAttachedToWindow(view: View) {
                view.scaleX = -1f//  mirror the page back when it was attached
            }
        }

        val recyclerView = getChildAt(0) as RecyclerView
        recyclerView.addOnChildAttachStateChangeListener(listener)
    }
}

Since it depends on the internal implementation of viewpager, it tested on androidx.viewpager2:viewpager2:1.0.0

Solution 4:

you can now show items horizontally in Recyclerview.

There have an option to show recyclerview items as reverse order.

RecyclerViewrecyclerView= findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(newLinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, true)); // last parameter is isReverse

If you want to show RecyclerView items like ViewPager paging you should to use this library:

https://github.com/lsjwzh/RecyclerViewPager

This codes will show RecyclerView as Reverse Order ViewPager.

Here is the code:

RecyclerViewPagerrecyclerViewPager= findViewById(R.id.recyclerViewPager);
recyclerViewPager.setLayoutManager(newLinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, true));


repositories {
    ...
    maven { url "https://jitpack.io" }
    ...
}

dependencies {
    ...
    compile 'com.github.lsjwzh.RecyclerViewPager:lib:v1.1.2@aar'
    ...
}

Post a Comment for "How To Reverse The Direction Of Viewpager To Left To Right Order?"