ViewPager的碎片消失了

时间:2013-08-28 08:46:32

标签: android android-viewpager navigation-drawer android-nested-fragment

我将DrawerLayoutViewPagerIndicator一起使用,但最后一次无法与DrawerLayout一起使用。打开应用程序后,viewpager会正确显示,但是一旦我在DrawerLayout中更改了片段,ViewPager的片段就会消失。

这是我的FragmentManager:

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

ViewPager中的第二个片段和第四个片段在那里它们并没有消失,而第一个,第三个和第五个片段消失了。我的代码有什么问题?

这是我的片段:

@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;
    }

}

1 个答案:

答案 0 :(得分:16)

getChildFragmentManager()GoogleMusicAdapter

一起使用
adapter = new GoogleMusicAdapter(getChildFragmentManager());

因为ViewPager使用的片段是嵌套片段。