Android : Implementation of two way Endless Viewpager

Hiren Patel picture Hiren Patel · Mar 29, 2016 · Viewed 11.5k times · Source

What I want:

I have been trying to implement two directional Endless viewpager in Android, Left to Right & Right to Left

What I did:

I have implemented Endless viewpager adapter, it works fine for right to left direction, I have set current item position by viewPager.setCurrentItem(Integer.MAX_VALUE/2);.

Reference:

enter image description here

Help would be appreciate.

Answer

pRaNaY picture pRaNaY · Apr 3, 2016

Try to check below FragmentPagerAdapter to get endless viewpager adapter :

public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public int getCount() {
    return Integer.MAX_VALUE;
}

@Override
public Fragment getItem(int position) {
    return getFragmentBasedOnPosition(position);
}

private Fragment getFragmentBasedOnPosition(int position) {
    int fragmentPos = position % 3; // Assuming you have 3 fragments
    switch(fragmentPos) {
        case 0:
        return Fragment1.newInstance();
        case 1:
        return Fragment2.newInstance();
        case 2:
        return Fragment3.newInstance();
    }
 }
}

I found solution here.

I hope its helps you.