9

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

这是我的片段管理器:

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

}
4

1 回答 1

18

getChildFragmentManager()GoogleMusicAdapter: _

adapter = new GoogleMusicAdapter(getChildFragmentManager());

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

于 2013-08-28T13:30:15.533 回答