1.初始化你要加载的 Fragment 界面。
/** * 初始化 页面 并且把对应的 Fragment new出来添加进一个集合里边。 */private void inviDates() { if (homeFragment == null) { homeFragment = new HomeFragment(); } if (mineFragment == null) { mineFragment = new MineFragment(); } if (orderFragment == null) { orderFragment = new OrderFragment(); } if (fabuFragment == null) { fabuFragment = new FabuFragment(); } fragments = new Fragment[]{homeFragment, fabuFragment, orderFragment, mineFragment}; //初始 展示的 Fragment inviView(); } private void inviView() { //判断加载的Fragment是不同的 if (currentTabIndex != index) { FragmentTransaction trx = getSupportFragmentManager().beginTransaction(); //判断当前的Fragment有没有添加进去 if (!fragments[index].isAdded()) { trx.add(R.id.fragment, fragments[index]); } //隐藏上一个
trx.hide(fragments[currentTabIndex]); trx.show(fragments[index]).commit();
currentTabIndex = index; }