Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Endless ViewPager android

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)

like image 407
artouiros Avatar asked May 21 '12 09:05

artouiros


People also ask

How to implement viewpager in Android?

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

What is the use of pageradapter in Android?

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.

How to identify previous and next pages of a viewpager?

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.

What is the use of pagertitlestrip in viewpager?

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.


1 Answers

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);
like image 182
FloRyan Avatar answered Sep 28 '22 07:09

FloRyan