5

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 current view etc.

the behavior i want is that after the first item is displayed, swiping left to right will display the next view left to the current item.

i searched a lot for clever way to implement this but no results..
thanks in advance.

dvrm
  • 3,749
  • 4
  • 34
  • 43

4 Answers4

8

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

Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
  • but every swipe i'll have to call setCurrentItem? i'll lose the viewPager advantage this way, no? – dvrm Nov 21 '12 at 10:33
  • if you reverse your list of view and set the current item to last view then you last view would be displayed.. now since you have reversed the list of views this last view would be the first view of your original view list... now user would not be able to move to right since you are on the last view.. user can only move left to see the 2nd last view (which was the 2nd viewin your original list), 3rd last view(which was the 3rd viewin your original list) and so on.. I guess that is what you wanted.. – Praful Bhatnagar Nov 21 '12 at 10:36
  • ooops you are right.. i notice this one second after writing and tried to delete my comment but you answered quicker..thanks!!! – dvrm Nov 21 '12 at 10:43
  • The problem here is that when you set the adapter for the viewpager, by default, it creates OffsetLimit*Fragments for "pre-loading" purposes. Then you set the currentItem to be the last one, thus asking the viewpager to create another OffsetLimit*Fragments... could not find a way around that... – baramuse Apr 30 '14 at 07:51
3

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

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
  • i thoght about this way but my views list is very dinamic and views are added while swiping according to swipe position so this approach of setCurrentItem will complicate it more.. – dvrm Nov 21 '12 at 10:35
  • You shouldn't need to dynamically add views; use Fragments instead and the support library will take care of their lifecycle for you. – Paul Lammertsma Nov 21 '12 at 10:55
  • what do you mean? i'll outline what i'm doing: in the vp(viewPager) i have one view that is listView (the higher level in the hirarchy). swiping considers the item of the list that was swiped and adds additional listView to the vp that displays the next level hirarchy and so on. swiping back removes the last listView. this works well but now i have the reverse the vp to left to right and i'm embarrassed.i'm looking for non "mamual" way. could you help me? my english is not so good, do you understand what do i mean? thanks so much for giving your time. – dvrm Nov 21 '12 at 11:55
  • I'm not exactly sure what you mean as you're talking about both a ViewPager and a ListView. I recommend reading [this Android Dev Blog article about the ViewPager using Fragments](http://android-developers.blogspot.nl/2011/08/horizontal-view-swiping-with-viewpager.html), [this blog post](http://tamsler.blogspot.nl/2011/10/android-viewpager-and-fragments.html) for a complete example, and [this question](http://stackoverflow.com/questions/10180539/working-with-viewpager-and-fragments) for some insightful remarks. – Paul Lammertsma Nov 21 '12 at 12:06
  • it's viewpager that contains lists. i'll learn your links and see if it helps. thanks very much! – dvrm Nov 21 '12 at 12:15
  • You're very welcome! I'd suggest asking a new question if you have specific problem. – Paul Lammertsma Nov 21 '12 at 13:32
0

you can now show items horizontally in Recyclerview.

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

RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(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:

RecyclerViewPager recyclerViewPager = findViewById(R.id.recyclerViewPager);
recyclerViewPager.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, true));


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

dependencies {
    ...
    compile 'com.github.lsjwzh.RecyclerViewPager:lib:v1.1.2@aar'
    ...
}
Ahamadullah Saikat
  • 4,437
  • 42
  • 39
0

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: Boolean
get() = scaleX == -1f
set(value) {
    if (value) {
        scaleX = -1f  //  mirror viewpager

        val listener = object : RecyclerView.OnChildAttachStateChangeListener {
            override fun onChildViewDetachedFromWindow(view: View) = Unit

            override fun onChildViewAttachedToWindow(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

vadiole
  • 337
  • 1
  • 6
  • 21