0

I am using a ViewPager with FragmentStatePagerAdapter to display images. I am able to successfully display images and now I am trying to implement "infinite scrolling". "Infinite scrolling" means that I am able to loop to the first image once I reach the last image & and I am to loop to the last image from the first image. Below is an illustration of "infinite scrolling" for clarification.

"infinite scrolling" : (... C <-> A <-> B <-> C <-> A ... )

My code implementation of this is below:

/* PagerAdapter class */
public class SlidePagerAdapter extends FragmentStatePagerAdapter {
    private int urlPicArryIndex = 0;

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

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

    @Override
    public Fragment getItem(int position) {

        System.err.println("orignal position = " + position);

        position = position % urlPicArray.size();
        urlPicArryIndex = position;

        System.err.println("urlPicArryIndex = " + urlPicArryIndex);
        System.err.println("position = " + position + ",     NUM_ITEMS = " + NUM_ITEMS);

        return PictureSectionFragment.newInstance(position, urlPicArray.get(urlPicArryIndex));
    }
}

Here are what variables are set to that is not displayed in the code snippet above:

NUM_ITEMS is set to 800

urlPicArray is an arrayList of images set in another method.

My code works for "infinite scrolling", however, I am concerned that this is not efficient. By setting NUM_ITEMS to 800 does this set aside this large amount of fragment space in memory? BTW I realize that this is not truly infinite scrolling, however, it should be sufficient for most real world use cases.

I am a just getting into android dev so any help / guidance would be greatly appreciated!

Hokie2014
  • 13
  • 1
  • 6

1 Answers1

0

Once you only need images, you don't really need Fragments here: simple Views should be enough (Read: BigNerdRanch: ViewPager Without Fragments). And there's already written antonyt/InfiniteViewPager or imbryk/LoopingViewPager(and plenty others) extension of ViewPager for doing it.

Check these questions:

Regarding your code, it leverages pretty much same idea, as everyone's else(and that's a good news!). What can be improved - change NUM_ITEMS with Integer.MAX_VALUE, if number of Views/Fragments is small - I'd avoid re-creation of them over and over again and replace them with already instantiated copies (i.e. store instantiated View/Fragment and reuse it later).

But again, check this answer: https://stackoverflow.com/a/12965787/1658267 - I think it's great.

Community
  • 1
  • 1
Konstantin Loginov
  • 14,994
  • 5
  • 54
  • 90
  • Thank your this is very helpful! What does using Integer.MAX_VALUE accomplish? Also does setting NUM_ITEMS to a large number cause it to be inefficient? – Hokie2014 Dec 28 '15 at 20:35
  • Big NUM_ITEMS and MAX_VALUE are equal from the efficient point of view, it's just a bit better code style (though both are "magic numbers). :-) – Konstantin Loginov Dec 28 '15 at 20:39