Pierpaolo Paolini
Pierpaolo Paolini

Reputation: 231

viewPager with tab in android

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??

Upvotes: 0

Views: 211

Answers (1)

Mr. Kevin Thomas
Mr. Kevin Thomas

Reputation: 847

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!

Upvotes: 1

Related Questions