0

hi i have a fragmentAdapter:

public class TitleAdapter extends FragmentPagerAdapter {

    private final String titles[] = new String[] { "Home", "Eventi"};
    private final Fragment frags[] = new Fragment[titles.length];
    Context context;
    public TitleAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Log.v("TitleAdapter - getPageTitle=", titles[position]);
        return titles[position];
    }

    @Override
    public Fragment getItem(int position) {
        Log.v("TitleAdapter - getItem=", String.valueOf(position));
        //return frags[position];
        switch (position) {
        case 0:
            return Home.newInstance(0, "aa");
        case 1:
            return Eventi.newInstance(1, "bbbb");
        }
        return null;
    }

    @Override
    public int getCount() {
        return frags.length;
    }
}

in each fragment (Home and Eventi) i have a list fragment. What i see in the logcat is that all the fragments are loaded when the activity starts. What i'd like to do is to load one fragment each time . how to do that??

1 Answers1

1

Sorry to disappoint, but you cannot load only a single fragment at a time with the Android library ViewPager. Generally in order to set how many fragments for the ViewPager to render, you would use this method public void setOffscreenPageLimit (int limit).

The problem though, is that the default and minimum value for the off-screen page limit is 1, which means that the ViewPager will render and keep in memory the fragments on either side of the currently selected fragment. You would think that you could set the limit to 0, but it will still default to 1 (see ViewPager.setOffscreenPageLimit(0) doesn't work as expected).

I assume you're forced to render at least 1 fragment ahead in order to ensure smooth scrolling. I hope this info helps!

Community
  • 1
  • 1
Mr. Kevin Thomas
  • 837
  • 2
  • 13
  • 21
  • thank you for the reply. So you are telling me that what i try to do is not possible right? I don't understand why the method getItem is called two times if you have two tab, three times if you have three tab and so on. It should be easy : if you are on fragment that has position 0 it should ONLY load that fragment and not all the others. – Pierpaolo Paolini Feb 16 '15 at 10:27
  • correct, it's not possible to force the viewpager to only load the current page. if you're facing performance issues with multiple views being rendered, perhaps you should simplify the views. good luck! – Mr. Kevin Thomas Feb 17 '15 at 23:38
  • ok . Do you know some tricks to do that?. Let me explain : if i have 6 tabs and in each tab i have a long list i will load all the 6 tabs when activity starts but in general i will click ONLY on one tab let's say the first one (the fragment at position zero) and i don't really understand why is not possible to load only that fragment. Anyway have you any suggestion to do that?? Thank you – Pierpaolo Paolini Feb 18 '15 at 00:18