Home » Android » android – ViewPager's Fragments disappear

android – ViewPager's Fragments disappear

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m using DrawerLayout with ViewPagerIndicator, however the last won’t work correctly with the DrawerLayout. Once I open the application, the viewpager is displayed correctly but once I change fragments in the DrawerLayout the ViewPager’s fragments disappear.

Here’s my FragmentManager:

FragmentManager fragmentManager = getSupportFragmentManager();
switch (arg2) {
    case 0:
        contentFragment = new Lebanon();
        break;
} 
fragmentManager.beginTransaction()
        .replace(R.id.content_frame, contentFragment).commit();

The second fragment and fourth in the ViewPager are there and they didn’t disappear, While the first, third and fifth disappear. What’s wrong in my code?

Here’s my Fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    viewta = inflater.inflate(R.layout.simple_tabs, container, false);
    SamsungLB = com.androidarabia.lebanon.SamsungLB.newInstance();
    HTCLB = com.androidarabia.lebanon.HTCLB.newInstance();
    LGLB = com.androidarabia.lebanon.LGLB.newInstance();
    SonyLB = com.androidarabia.lebanon.SonyLB.newInstance();
    SearchLB = com.androidarabia.lebanon.SearchLB.newInstance();
    this.getSherlockActivity().getSupportActionBar()
            .setHomeButtonEnabled(true);
    prefs = this.getSherlockActivity().getSharedPreferences(prefName,
            Context.MODE_PRIVATE);
    adapter = new GoogleMusicAdapter(this.getSherlockActivity()
            .getSupportFragmentManager());
    pager = (ViewPager) viewta.findViewById(R.id.pager);

    pager.setPageTransformer(true, new ZoomOutPageTransformer());
    indicator = (TabPageIndicator) viewta.findViewById(R.id.indicator);
    pager.setCurrentItem(0);
    pager.setAdapter(adapter);
    pager.setOffscreenPageLimit(4);
    indicator.setViewPager(pager);
    return viewta;
}

private void addtabs(ViewPager pager2) {
    // TODO Auto-generated method stub
    pager2.setAdapter(adapter);
    pager2.setOffscreenPageLimit(4);
    indicator.setViewPager(pager);
}

class GoogleMusicAdapter extends FragmentStatePagerAdapter {

    public GoogleMusicAdapter(FragmentManager fm) {
        super(fm);
        notifyDataSetChanged();
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
        case 0:
            return SamsungLB;
        case 1:
            return HTCLB;
        case 2:
            return LGLB;
        case 3:
            return SonyLB;
        case 4:
            return SearchLB;

        }
        notifyDataSetChanged();

        return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
    }

    @Override
    public CharSequence getPageTitle(int position) {

        return CONTENT[position % CONTENT.length].toUpperCase();
    }

    @Override
    public int getCount() {

        return CONTENT.length;
    }

}
How to&Answers:

Use getChildFragmentManager() with the GoogleMusicAdapter:

adapter = new GoogleMusicAdapter(getChildFragmentManager());

because the fragments used by the ViewPager are nested fragments.