lundi 4 mai 2015

How to set order of viewpager pages?

So I have a Viewpager class which is adding 3 fragments that each have their own layouts. Lets call them Layout/Fragment A, Layout/Fragment B, and Layout/Fragment C.

ViewPager Class:

public class viewpagerMainPagesActivity extends FragmentActivity {
DescriptionPageAdapter pageAdapter;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewpager_descriptionpages);

    List<Fragment> fragments = getFragments();
    pageAdapter = new DescriptionPageAdapter(getSupportFragmentManager(), fragments);
    ViewPager pager = (ViewPager)findViewById(R.id.viewpager_descriptionpages);
    pager.setAdapter(pageAdapter);

}
private List<Fragment> getFragments() {
    List<Fragment> fList = new ArrayList<Fragment>();
    fList.add(fragmentA.newInstance());
    fList.add(fragmentB.newInstance());
    fList.add(fragmentC.newInstance());

    return fList;
}
}

Now when I call this ViewPager class, it is showing them in the order they are added, that is, it opens onto Fragment A. It is FragmentA as the first screen, FragmentB when you swipe towards the right (2nd screen), and then FragmentC as the last screen on the right.

A - B - C

But I want to have the ViewPager keep this order of the fragments, but open onto FragmentB, so the user can Swipe Right to Fragment A, or swipe left to fragment C.

Can anyone point me in the right direction to do this?

Aucun commentaire:

Enregistrer un commentaire