Load only one Fragment in ViewPager
Asked Answered
F

3

13

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

Fulani answered 12/7, 2013 at 9:28 Comment(1)
W
4

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.

Waxwork answered 12/7, 2013 at 9:33 Comment(0)
H
3

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.

Hellraiser answered 13/8, 2013 at 6:33 Comment(0)
O
0

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;
Output answered 25/12, 2013 at 3:45 Comment(1)
I don't think this this would work. getItem will be called twice with index and index+1 depending on how you swipe your viewsArboreal

© 2022 - 2024 — McMap. All rights reserved.