15

Possible Duplicate:
ViewPager as a circular queue / wrapping

I have three fragment classes in my code. FirstActivity,SecondActivity,ThirdActivity i want to swipe these three activities.. these three are extend from Fragment class

public class ViewPagerFragmentActivity extends FragmentActivity {
private PagerAdapter mPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.viewpager_layout);
    initialisePaging();
    }

/**
 * Initialize the fragments to be paged
 */
List<Fragment> fragments = new Vector<Fragment>();
ViewPager pager;
private void initialisePaging() {
    fragments.add(Fragment.instantiate(this, FirstActivity.class.getName()));
    fragments.add(Fragment.instantiate(this, SecondActivity.class.getName()));
    fragments.add(Fragment.instantiate(this, ThirdActivity.class.getName()));
    this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);
    pager = (ViewPager) super.findViewById(R.id.viewpager);
    pager.setAdapter(this.mPagerAdapter);

}

}

and here is my FragmentPageAdapter class

public class MyPagerAdapter extends FragmentPagerAdapter {

private final List<Fragment> fragments;


public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
    super(fm);
    this.fragments = fragments;
}


@Override
public Fragment getItem(int position) {
    System.out.println("position"+position);
    return this.fragments.get(position);
}


@Override
public int getCount() {
    return this.fragments.size();
}

}

my code is working in this way A<->B<->c but my requirement is to run like this wayC<->A<->B<->C<-A-> that is in circular fragments are swipable...

Community
  • 1
  • 1
Venkat
  • 3,268
  • 6
  • 36
  • 60
  • Sample gist: https://gist.github.com/gelldur/051394d10f6f98e2c13d and how it works: https://youtu.be/7up36ylTzXk – Dawid Drozd Oct 06 '15 at 22:12

2 Answers2

19

Circular ViewPager is possible. With the help of ViewPager as a circular queue / wrapping this link have coded like this. Hope it helps what you needed.

The CircluarPagerAdapter class:

>

 public class CircularPagerAdapter extends PagerAdapter{

private int[] pageIDsArray;
private int count;

public CircularPagerAdapter(final ViewPager pager, int... pageIDs) {
    super();
    int actualNoOfIDs = pageIDs.length;
    count = actualNoOfIDs + 2;
    pageIDsArray = new int[count];
    for (int i = 0; i < actualNoOfIDs; i++) {
        pageIDsArray[i + 1] = pageIDs[i];
    }
    pageIDsArray[0] = pageIDs[actualNoOfIDs - 1];
    pageIDsArray[count - 1] = pageIDs[0];

    pager.setOnPageChangeListener(new OnPageChangeListener() {

        public void onPageSelected(int position) {
            int pageCount = getCount();
            if (position == 0){
                pager.setCurrentItem(pageCount-2,false);
            } else if (position == pageCount-1){
                pager.setCurrentItem(1,false);
            }
        }

        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            // TODO Auto-generated method stub
        }

        public void onPageScrollStateChanged(int state) {
            // TODO Auto-generated method stub
        }
    });
}

public int getCount() {
    return count;
}

public Object instantiateItem(View container, int position) {
    LayoutInflater inflater = (LayoutInflater) container.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    int pageId = pageIDsArray[position];
    View view = inflater.inflate(pageId, null);
    ((ViewPager) container).addView(view, 0);
    return view;
}

@Override
public void destroyItem(View container, int position, Object object) {
    ((ViewPager) container).removeView((View) object);
}

@Override
public void finishUpdate(View container) {
    // TODO Auto-generated method stub
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((View) object);
}

@Override
public void restoreState(Parcelable state, ClassLoader loader) {
    // TODO Auto-generated method stub
}

@Override
public Parcelable saveState() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void startUpdate(View container) {
    // TODO Auto-generated method stub
}
}

and this is your main class:

>

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewPager myPager = (ViewPager) findViewById(R.id.conpageslider);
    PagerAdapter adapter = new CircularPagerAdapter(myPager, new int[]{R.layout.first_activity, R.layout.second_activity, R.layout.third_activity});
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(3);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


}
Community
  • 1
  • 1
Zombie
  • 1,945
  • 2
  • 20
  • 33
  • The problem is that if you change something on view1 for example a textview, that change will be notice in the dummy view1(which wont have the same text)...Any ideas? – Dayerman Nov 30 '12 at 00:32
  • @Alice I am not able to perform events when I go according to your way. – Rahul Baradia Oct 17 '13 at 13:45
  • 5
    FYI, this doesn't work that well..when it does the jump from the end, it'll be very non-smooth because it's a jump. It doesn't look circular...it looks glitchy :-P – kenyee May 21 '14 at 15:48
  • Now I feel the same. Thanks Kenyee!! – Zombie Jan 23 '15 at 06:53
14

You can try something like this :

1) Make getCount() return a huge value ( a number of swipe way higher than what your user might use )

2) Make getItem return something like this.fragments.get(position % fragments.size())

3) Make your pager start somewhere in the middle of the range defined by getCount() ( To allow swiping from A to C at the beginning ), make sure to choose a value which will give you the first fragment % 3

I'm aware this solution seems a bit dirty but I cant find a better one at the moment

Martin Konecny
  • 50,691
  • 18
  • 119
  • 145
Furzel
  • 596
  • 6
  • 18
  • 3) i am unable to understand the 3rd point plz explain me clearly @Furzel – Venkat Jul 13 '12 at 08:34
  • Let's say you choose a size of 10000, you want your viewpager to start at position 5001 since 5001 % 3 = 0 the first fragment will be A and your users will be able to swipe 5000 view back or 5000 view forward. – Furzel Jul 13 '12 at 09:27
  • this turn to be the simple and quickest solution that work on 99% of use cases. – shem Apr 02 '13 at 13:44
  • 2
    Does not work if you use ViewPagerIndicator in conjunction with ViewPager. Indicator attempts to populate title views for all your `getCount()` pages which quickly results in OutOfMemory error – Alex Semeniuk Nov 26 '13 at 15:39
  • 2
    If you do this and call pager.setCurrentItem, you'll also lock up because it tries to calculate the viewpager width and it'll be gigantic so it takes forever :-P – kenyee May 21 '14 at 15:19