1

So i have the next problem with FragmentPagerAdapter. I have a TabLayout with 3 tabs representing 3 fragments that i can switch. So when i switch to third fragment, for some reason the first one disappears (or its view). Does anyone know how to fix this problem? Thanks in advance.

public class SectionsPagerAdapter extends FragmentPagerAdapter {

private static final int FRAGMENT_COUNT = 3;

private final List<Fragment> listOfFragments = new ArrayList<>();

public SectionsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    return listOfFragments.get(position);
}

@Override
public int getCount() {
    return FRAGMENT_COUNT;
}

public void addFragment(Fragment fragment) {
    listOfFragments.add(fragment);
}


}

This is the code for FragmentPagerAdapter.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
RadoVidjen
  • 432
  • 7
  • 17

4 Answers4

1

And i think i fixed this :) Only thing i did is to override the FragmentPagerAdapter destroyItem() method, with empty body (no super).

RadoVidjen
  • 432
  • 7
  • 17
0

I don't know what is your problem. but if in your app only 3 fragments are there than you can try setOffscreenPageLimit method of ViewPager

ViewPager.setOffscreenPageLimit(3);
Bhavesh Rangani
  • 1,490
  • 12
  • 23
  • Thanks for comment! The problem is that for some reason if you have three fragments, and lets say you setCurrentItem(1) (meaning that second fragment is default), then go to the last fragment (third) and then go back to the first fragment, there is no first fragment, looks like its removed for some reason (or at least its layout). As i mentioned in my comment, i have fixed it with overriding the destroyItem() from FragmentPagerAdapter. – RadoVidjen Jun 07 '18 at 10:23
  • okay great. and post your solution as an answer so if any other visit this question then your answer will help them to resolve his problem – Bhavesh Rangani Jun 07 '18 at 10:29
0

One of the problems here is a slightly confusing API.

In FragmentPagerAdapter, getItem(int position) actually means "create item". In other words, you shouldn't try to manually cache the Fragments inside a list in your Adapter.

@Override
public Fragment getItem(int position) {
    return listOfFragments.get(position); //No! don't do this
}

Even though it may see counter-intuitive, you should actually create a new instance of the Fragment you want inside getItem, sticking very closely to the official Google example:

@Override 
public Fragment getItem(int position) {
    switch (position) {
        case 0: 
            return FirstFragment.getInstance();
        case 1:
            return SecondFragment.getInstance(); 
    }
}

Otherwise you will run into problems where the FragmentManager's cache and your own List<Fragment> cache are out-of-sync. In short, caching of Fragments is handled by the FragmentManager and you don't need to roll-your-own caching.

David Rawson
  • 20,912
  • 7
  • 88
  • 124
-1

I don't know really what happen but this is my code, hope this help you

public class AdapterFragmentViewPager extends FragmentPagerAdapter {

    private List<Fragment> fragmentList = new ArrayList<>();


    public AdapterFragmentViewPager(FragmentManager fm) {
        super(fm);
    }

    public void addFragment(Fragment fragment) {
        fragmentList.add(fragment);
    }

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }
}

and this is my Tablayout:

private AdapterFragmentViewPager adapterFragmentViewPager = new AdapterFragmentViewPager(getSupportFragmentManager());
TabLayout.Tab home = tabLayout.newTab();
        tabLayout.addTab(home);

        TabLayout.Tab newest = tabLayout.newTab();
        tabLayout.addTab(newest);

        TabLayout.Tab author = tabLayout.newTab();
        tabLayout.addTab(author);

        TabLayout.Tab category = tabLayout.newTab();
        tabLayout.addTab(category);

        TabLayout.Tab saved = tabLayout.newTab();
        tabLayout.addTab(saved);


        adapterFragmentViewPager.addFragment(FragmentHome.newInstance());
        adapterFragmentViewPager.addFragment(FragmentQuote.newInstance());
        adapterFragmentViewPager.addFragment(FragmentAuthor.newInstance());
        adapterFragmentViewPager.addFragment(FragmentCategory.newInstance());
        adapterFragmentViewPager.addFragment(FragmentFavorites.newInstance());
        viewPager.setAdapter(adapterFragmentViewPager);
        tabLayout.setupWithViewPager(viewPager);
Hoàng Vũ Anh
  • 647
  • 1
  • 8
  • 24