public abstract class FragmentStatePagerAdapter extends PagerAdapter {
@Override
public Object instantiateItem(ViewGroup container, int position) {
// If we already have this item instantiated, there is nothing
// to do. This can happen when we are restoring the entire pager
// from its saved state, where the fragment manager has already
// taken care of restoring the fragments we previously had instantiated.
if (mFragments.size() > position) {
Fragment f = mFragments.get(position);
if (f != null) {
return f;
}
}
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
Fragment fragment = getItem(position);
if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
if (mSavedState.size() > position) {
Fragment.SavedState fss = mSavedState.get(position);
if (fss != null) {
fragment.setInitialSavedState(fss);
}
}
while (mFragments.size() <= position) {
mFragments.add(null);
}
fragment.setMenuVisibility(false);
fragment.setUserVisibleHint(false);
mFragments.set(position, fragment);
mCurTransaction.add(container.getId(), fragment);
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
Fragment fragment = (Fragment) object;
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
if (DEBUG) Log.v(TAG, "Removing item #" + position + ": f=" + object
+ " v=" + ((Fragment)object).getView());
while (mSavedState.size() <= position) {
mSavedState.add(null);
}
mSavedState.set(position, fragment.isAdded()
? mFragmentManager.saveFragmentInstanceState(fragment) : null);
mFragments.set(position, null);
mCurTransaction.remove(fragment);
}
}
如果非要这么玩, 请参考
FragmentStatePagerAdapter
的管理方式. 关键代码如下:除此之外,
如果非要这么玩, 请参考Activity
有 NavigateUp(向上导航) 和 BackPressed(回退键) 的功能, 因此, 还要管理Fragment
的回退栈(和Activity
的回退栈是不一样的), 即调用FragmentTransaction#addToBackStack(String)
FragmentStatePagerAdapter
的管理方式. 关键代码如下:🎜 rrreee 🎜除此之外,Activity
有 NavigateUp(向上导航) 和 BackPressed(回退键) 的功能, 因此, 还要管理Fragment
的回退栈(和Activity
的回退栈是不一样的), 即调用FragmentTransaction#addToBackStack(String)
.🎜建议同上面的回答一样,尽量不要这样搞。
希望你可以分析一下需要处理的几个页面,是否是
同样性质
的。比如说网易新闻,不同频道所展现的内容是类似的,所以使用Fragment比较合适。但是设置项中,各设置项中内容比较独立,使用不同的Activity比较好。