private
FragmentManager mFm;
private
Fragment mContent; //正在显示的fragment
* 设置默认的fragment,即第一次加载界面;
*/
private
void
setDefaultFragment(Fragment fm) {
mFm = getFragmentManager();
FragmentTransaction mFragmentTrans = mFm.beginTransaction();
mFragmentTrans.add(R.id.id_addfram, fm).commit();
mContent = fm;
}
/**
* 修改显示的内容 不会重新加载
*/
public
void
switchContent(Fragment from,Fragment to) {
if
(mContent != to) {
s//将下一个设置为正在显示的
mContent = to;
FragmentTransaction transaction = mFm.beginTransaction();
// 先判断即将要跳到的fragment是否被add过
if
(!to.isAdded()) {
// 隐藏当前的fragment,add下一个到Activity中
transaction.hide(from).add(R.id.id_addfram, to).commit();
}
else
{
// 隐藏当前的fragment,显示下一个fragment
transaction.hide(from).show(to).commit();
}
}
}
/**
* 修改显示的内容 但会重新加载 *
*/
public
void
switchContent2(Fragment to){
FragmentTransaction transaction = mFm.beginTransaction();
transaction.replace(R.id.id_addfram,to);
//transaction.addToBackStack(null);
transaction.commit();
}