美文网首页
Fragment-commit

Fragment-commit

作者: 河马过河 | 来源:发表于2019-06-13 17:01 被阅读0次
 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();
    }
``

相关文章

网友评论

      本文标题:Fragment-commit

      本文链接:https://www.haomeiwen.com/subject/kansfctx.html