Question:
Good evening. I have three fragments, and I need to flip through them in the ViewPager. And so that at startup the second fragment opens immediately, so that you have to flip through the backwipe to the first, and just swipe to the third fragment.
I wrote the following code in the adapter, it turned out to be crap:
FragmentManager fm = getSupportFragmentManager();
mViewPager.setAdapter(new FragmentStatePagerAdapter(fm) {
@Override
public Fragment getItem(int position) {
position = 2;
if(position == 1){
return new FirstFragment();
}
else if(position == 2) return new SecondFragment();
else return new ThirdFragment();
}
@Override
public int getCount() {
return 3;
}
});
mViewPager – ViewPager, declared earlier.
At the moment, it starts from the first fragment, or rather, the first position, but the second fragment is displayed. If you scroll further, the same second fragment is displayed. I do not know why.
What do you advise?
Answer:
To open the viewpager
immediately on the desired "page" call after you put the adapter
into it:
viewpager.setCurrentItem(num)