Let's say I have two pages in my viewpager, is it any way to move from page 2 to page 1, but doing this like user is accesing page 3 (with all the animation)
There are three important steps to implement ViewPager: 1.) A layout (that contains ViewPager) for the MainActivity. 2.) FragmentPagerAdapter/FragmentStatePagerAdapter class which controls the fragments to be shown on page swipes. 3.)
By the implementation of a PagerAdapter, we make the views which allows the user to flip left and right through pages of data. ViewPager is most often used in conjunction with Fragment, which is a useful way to supply and manage the lifecycle of each page.
PagerTitleStrip helps to identify previous, current and next pages of a viewpager. PagerTitleStrip displays next, current and previous page titles of view pager at top or bottom of the screen. PagerTitleStrip gets page title from view pager adapter.
ViewPager is mainly used for creating Sliding tabs. You can add PagerTitleStrip to ViewPager in layout xml file to display title for each page. PagerTitleStrip helps to identify previous, current and next pages of a viewpager.
You can achieve this by using a custom FragmentPagerAdapter that provides the same fragments over and over again:
private class EndlessPagerAdapter extends FragmentPagerAdapter {
private EndlessPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if (position % 2 == 0) {
return fragmentOne;
} else {
return fragmentTwo;
}
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
}
Set the adapter for your ViewPager and start somewhere in the middle to allow almost endless swiping in either direction:
mViewPager.setAdapter(new EndlessPagerAdapter(getChildFragmentManager()));
mViewPager.setCurrentItem(Integer.MAX_VALUE/2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With