加载另一个片段

时间:2015-09-10 11:43:51

标签: android android-fragments android-viewpager navigation-drawer

NavigationDrawer我正在拍摄两张碎片。在每个片段中,我都有标签滑块。当我第一次点击数据时,两个片段中都有数据,但是当我从一个片段进入另一个片段时,两个片段中的数据都为空。我检查了片段的保存状态,但问题仍未解决。知道我们如何解决这个问题吗?

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null) {
    setupDrawerContent(navigationView);
    mFragmentManager = getSupportFragmentManager();
    mFragmentTransaction = mFragmentManager.beginTransaction();
    mFragmentTransaction.replace(R.id.frame, new LoginFragment()).commit();
}

private void setupDrawerContent(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {@Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            System.out.println("Navigation Drawer Item Clciked ---->" + navDrawerClickedItem);
            Fragment fragment = null;
            switch (menuItem.getItemId()) {
                case R.id.roof:
                    if (navDrawerClickedItem == menuItem.getItemId()) {
                        mDrawerLayout.closeDrawer(Gravity.LEFT);
                        System.out.println("New roof Fragment already loaded");
                    } else {
                        navDrawerClickedItem = menuItem.getItemId();
                        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.frame, new RoofTabFragment()).commit();
                        System.out.println("New roof Fragment Created");
                    }
                    break;
                case R.id.floor:
                    if (navDrawerClickedItem == menuItem.getItemId()) {
                        mDrawerLayout.closeDrawer(Gravity.LEFT);
                        System.out.println("Floor Fragment already loaded");
                    } else {
                        navDrawerClickedItem = menuItem.getItemId();
                        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.frame, new FloorTabFragment()).commit();
                        System.out.println("Floor Fragment Created");
                    }
                    break;
                case R.id.signOut:
                    LogMeOut();
                    break;
            }
            if (fragment != null) {
                mFragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
            } else {
                Log.e("MainActivity", "Error in creating fragment");
            }
            menuItem.setChecked(true);
            setTitle(menuItem.getTitle());
            mDrawerLayout.closeDrawers();
            return false;
        }
    });
}

1 个答案:

答案 0 :(得分:2)

问题已解决,将body {font-family:sans-serif;background-image: url("pictures/rain.jpg");} #b1 {border-style:solid;border-color:#7f7f7f;width:310px;text-align:center;padding-top:3px;padding-bottom:3px;background-color:#7f7f7f;opacity:0.8;position:fixed;} #b2 {border-style:solid;border-color:#7f7f7f;width:310px;text-align:center;padding-top:3px;padding-bottom:3px;background-color:#7f7f7f;opacity:0.8;position:fixed;left:323px;} #b3 {border-style:solid;border-color:#7f7f7f;width:310px;text-align:center;padding-top:3px;padding-bottom:3px;background-color:#7f7f7f;opacity:0.8;position:fixed;left:637.5px;} #b4 {border-style:solid;border-color:#7f7f7f;width:310px;text-align:center;padding-top:3px;padding-bottom:3px;background-color:#7f7f7f;opacity:0.8;position:fixed;left:952px;} 替换为getFragmentManager(中的getChildFragmentManager()