多个fragment的切换 /** * 使用show() hide()切换页面 * 显示fragment */ private void showFragment(int tabIndex){ currentIndex = tabIndex; FragmentTransaction transaction = fragmentManager.beginTransaction(); //如果之前没有添加
/** * 使用show() hide()切换页面 * 显示fragment */ private void showFragment(int tabIndex){ currentIndex = tabIndex; FragmentTransaction transaction = fragmentManager.beginTransaction(); //如果之前没有添加过 if(!fragmentList.get(tabIndex).isAdded()){ transaction .hide(currentFragment) .add(R.id.rlContent,fragmentList.get(tabIndex),""+tabIndex); //第三个参数为添加当前的fragment时绑定一个tag }else{ transaction .hide(currentFragment) .show(fragmentList.get(tabIndex)); } currentFragment = fragmentList.get(tabIndex); transaction.commit(); }