使用 FragmentStatePagerAdapter 保持对我自己的适配器中的片段的引用怎么样?然后我将它们都保存在内存中,但也可以完全控制删除。这种方法有什么不好的地方吗?以后会遇到问题吗?我也进行了更改,因此片段作为对象而不是类传递,从而将启动留在寻呼机之外。这有什么问题吗?
附上我的适配器代码:
public static class TabsAdapter extends FragmentStatePagerAdapter implements
ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<Fragment> mTabs = new ArrayList<Fragment>();
public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = activity.getSupportActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Fragment fragment) {
tab.setTag(fragment);
tab.setTabListener(this);
mTabs.add(fragment);
mActionBar.addTab(tab);
notifyDataSetChanged();
}
public void replaceTab(ActionBar.Tab tab, Fragment fragment, int position) {
tab.setTag(fragment);
tab.setTabListener(this);
mTabs.set(position, fragment);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public Fragment getItem(int position) {
return mTabs.get(position);
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Object tag = tab.getTag();
for (int i = 0; i < mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
mViewPager.setCurrentItem(i);
}
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public int getItemPosition(Object object){
return PagerAdapter.POSITION_NONE;
}
}