Load only one Fragment in ViewPager

最后都变了- 提交于 2019-12-21 07:48:07

问题


I have used ViewPager with Fragment.
But it loads two pages at time.
Is there any way to load only one page in viewpager ?


回答1:


This might be the thing you are looking for:

  mPager.setOffscreenPageLimit(n); // where n is the number of offscreen pages you want to load.

**The minimum value of it can be "1" as you said. ** check this link and also read the comments.




回答2:


setOffScreenPageLimit method has a limit of minimum 1. If you'll try to set smaller limit than this, it will use its default value which is 1.




回答3:


You can try to do manual your adapter such as

public int currentIndex;

@Override
public Fragment getItem(int index) {
    if(position == currentIndex){
        return new EmptyFragment();
    }else{
        return new YourNormalFragment();
    }
}

and becareful to modify

    yourViewPager.setCurrentItem(index);

along with

    yourAdapter.currentIndex = index;


来源:https://stackoverflow.com/questions/17611707/load-only-one-fragment-in-viewpager

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