Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create circular viewpager? [duplicate]

Tags:

android

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

like image 455
Venkat Avatar asked Jul 13 '12 07:07

Venkat


2 Answers

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;
}


}
like image 153
Zombie Avatar answered Oct 22 '22 12:10

Zombie


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

like image 14
Furzel Avatar answered Oct 22 '22 11:10

Furzel