防止在Sherlock Fragment Activity上重新创建Android片段

时间:2013-12-10 06:54:20

标签: android android-fragments fragment

我有一个扩展SherlockFragmentActivity的活动,它有3个片段。每次我刷到第三个片段,然后刷回第一个片段,我意识到第一个片段重新加载。这是我的代码

    ActionBar ab = getSupportActionBar();
    FragmentManager fm = getSupportFragmentManager();
    ArrayList<Fragment> f = new ArrayList<Fragment>();

    f.add(new FragmentMe());
    f.add(new FragmentBrowse());
    f.add(new FragmentFeed());

    FragmentAdapter adapter = new FragmentAdapter(fm, f);
    pager.setAdapter(adapter);

    pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
        @Override
        public void onPageSelected(int position) {              
            super.onPageSelected(position);
            ab.setSelectedNavigationItem(position);         
        }
    });
    ActionBar.TabListener tabListener = new ActionBar.TabListener() {

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {              
        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            pager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
    };
    ab.addTab(ab.newTab().setText("Me").setTabListener(tabListener));
    ab.addTab(ab.newTab().setText("Browse").setTabListener(tabListener));
    ab.addTab(ab.newTab().setText("Feed").setTabListener(tabListener));

这是我的FragmentAdapter代码

public class FragmentAdapter extends FragmentStatePagerAdapter{

ArrayList<Fragment> fragment;

public FragmentAdapter(FragmentManager fm, ArrayList<Fragment> fragment) {
    super(fm);
    this.fragment = fragment;
}

@Override
public Fragment getItem(int pos) {
    return fragment.get(pos);
}

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

@Override
public CharSequence getPageTitle(int position) {
    return "Section " + (position + 1);
}

}

任何帮助将不胜感激

0 个答案:

没有答案