1

I know there are a few questions on infinite paging, but I am still having difficulty in getting any of them to work. (I am working in Android Studio)

I am trying to get the InfiniteViewPager from https://github.com/antonyt/InfiniteViewPager up and running.

I am trying to get this working so that I am able to swipe across 4 pages/fragments (A, B, C, D), with functionality for the right swipe on D to return to A, and a left swipe on A to return to D.

I've imported the InfinitePagerAdapter and InfiniteViewPager Java classes, and added the InfiniteViewPager to my activity_main.xml file.

However, I'm stuck with where I need to "Wrap your existing PagerAdapter with the InfinitePagerAdapter".

Here is my code:

public class MainActivity extends AppCompatActivity {

    PagerAdapter mPagerAdapter;
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mPagerAdapter);
        //Cannot resolve symbol adapter
        PagerAdapter wrappedAdapter = new InfinitePagerAdapter(adapter);
        mViewPager.setAdapter(wrappedAdapter);

        mViewPager.setOffscreenPageLimit(5);
    }

    public class PagerAdapter extends FragmentStatePagerAdapter {

        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            //getItem is called to instantiate the fragment for the given page.
            switch (position) {

                default:
                    return Fragment1.newInstance();
                case 1:
                    return Fragment2.newInstance();
                case 2:
                    return Fragment3.newInstance();
                case 3:
                    return Fragment4.newInstance();
            }
        }

        @Override
        public int getCount() {
            // Show number of total pages.
            return 4;
        }
    }
}

I'm not exactly sure what I should be replacing adapter with, or if it should be pulled from the other Java Classes. Additionally is there anything else I need to add/change to get this working?

Michael Zhang
  • 13
  • 1
  • 5
  • have a look at the demo here and you will see the reference in the code where it says (// wrap pager to provide infinite paging with wrap-around) -- https://github.com/antonyt/InfiniteViewPager/tree/master/demo/src/main/java/com/antonyt/infiniteviewpager – Tasos Jan 20 '16 at 05:44
  • @Tasos thanks, I'll have a look now and let you know how it goes - knew to coding and wasn't too sure how to use Github either. – Michael Zhang Jan 20 '16 at 05:49
  • @Tasos so I understand that the demo uses positions in order to recognise what view to display after each swipe. The demo creates a new instance of ColourFragment which then changes depending on the position. How can I change that to creating a new instance of the different fragments I have based on position? Thanks. – Michael Zhang Jan 20 '16 at 06:15
  • not sure about that. what you can do is join github and raise an issue -- https://github.com/antonyt/InfiniteViewPager/issues -- i had a look at some of them and this one -- https://github.com/antonyt/InfiniteViewPager/issues/11 -- you may not need the (mViewPager.setOffscreenPageLimit(5);) – Tasos Jan 20 '16 at 06:40
  • @MichaelZhang, can you please check http://stackoverflow.com/questions/7766630/changing-viewpager-to-enable-infinite-page-scrolling – Hiren Patel Jan 20 '16 at 07:28

1 Answers1

0

I've got it working after a bit of tinkering with the demo - code is below:

public class MainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        PagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override
            public int getCount() {
                return 4;
            }

            @Override
            public Fragment getItem(int position) {
                final ArrayList<Fragment> fragmentPosition = new ArrayList<>();
                fragmentPosition.add(new Fragment1());
                fragmentPosition.add(new Fragment2());
                fragmentPosition.add(new Fragment3());
                fragmentPosition.add(new Fragment4());
                Fragment fragment = fragmentPosition.get(position);
                Bundle args = new Bundle();
                fragment.setArguments(args);
                return fragment;
            }
        };

        // wrap pager to provide infinite paging with wrap-around
        PagerAdapter wrappedAdapter = new InfinitePagerAdapter(adapter);

        // actually an InfiniteViewPager
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(wrappedAdapter);

    }
}
Michael Zhang
  • 13
  • 1
  • 5