6

I have 3 fragments that are managed by an FragmentPagerAdapter, set to a viewPager.

I want to load fragments one by one, but when the onCreate method of FragmentActivity is executed, the 2 first fragments are executed (onCreateView method).

I have tried to limit fragments loading with the setOffscreenPageLimit method but nothing change.

mPagerAdapter =  new MyPagerAdapter(super.getSupportFragmentManager(), fragments);
pager = (ViewPager) super.findViewById(R.id.tabviewpager);
pager.setOffscreenPageLimit(0);
pager.setAdapter(this.mPagerAdapter);

Thank you for your help .

johann
  • 1,115
  • 8
  • 34
  • 60

1 Answers1

8

The minimum for OffscreenPageLimit is set to 1 in the ViewPager source code:

private static final int DEFAULT_OFFSCREEN_PAGES = 1;
....
public void setOffscreenPageLimit(int limit) {
    if (limit < DEFAULT_OFFSCREEN_PAGES) {
        Log.w(TAG, "Requested offscreen page limit " + limit + " too small; defaulting to " +
                DEFAULT_OFFSCREEN_PAGES);
        limit = DEFAULT_OFFSCREEN_PAGES;
    }
    if (limit != mOffscreenPageLimit) {
        mOffscreenPageLimit = limit;
        populate();
    }
}
antew
  • 7,468
  • 3
  • 23
  • 17
  • Thank you. Is there another way to achieve that without using setOffscreenPageLimit ? – johann Nov 27 '12 at 02:55
  • 2
    Do the fragments really need to be loaded one by one? If so, you might want to use something other than a `ViewPager` with an `Adapter`. If you really want a `ViewPager` you could use an [OnPageChangeListener](http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html) on the ViewPager and wait til the `Fragment` is visible to do whatever you need. You would just have to have do to some basic initialization in the `onCreateView` and then do the intensive work when the `Fragment` becomes visible. – antew Nov 27 '12 at 03:09
  • Thank you for your precious help ! I ll try with OnPageChangeListener. – johann Nov 27 '12 at 03:14