Android: limit fragments loading with a viewPager

半城伤御伤魂 提交于 2020-02-02 02:47:06

问题


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 .


回答1:


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();
    }
}


来源:https://stackoverflow.com/questions/13560550/android-limit-fragments-loading-with-a-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!