public void showFragment(int position) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment fragment = null;
switch (position) {
case 0:
fragment = fm.findFragmentByTag("videoListFragment");
if (fragment == null) {
fragment = new VideoListFragment();
ft.add(R.id.ll_activity_home_content, fragment, "videoListFragment");
Log.e("lll", "videoListFragment 新建");
}
break;
case 1:
fragment = fm.findFragmentByTag("photoListFragment");
if (fragment == null) {
fragment = new PhotoListFragment();
ft.add(R.id.ll_activity_home_content, fragment, "photoListFragment");
Log.e("lll", "photoListFragment 新建");
}
break;
case 2:
fragment = fm.findFragmentByTag("toolsFragment");
if (fragment == null) {
fragment = new ToolsFragment();
ft.add(R.id.ll_activity_home_content, fragment, "toolsFragment");
Log.e("lll", "toolsFragment 新建");
}
break;
case 3:
fragment = fm.findFragmentByTag("settingsFragment");
if (fragment == null) {
fragment = new SettingsFragment();
ft.add(R.id.ll_activity_home_content, fragment, "settingsFragment");
Log.e("lll", "settingsFragment 新建");
}
break;
default:
break;
}
if (currentFragment != null) {
if(fragment==currentFragment){
return ;
}
ft.show(fragment).hide(currentFragment);
}
currentFragment = fragment;
ft.commitAllowingStateLoss();
}
``
网友评论