在Android中导航到下一页时显示白屏的片段

时间:2018-03-27 13:34:51

标签: android android-layout android-fragments

当我尝试从Home_Fragment屏幕移动到Second Fragment屏幕时,我得到一个白色背景屏幕然后当我再次尝试从Second Fragment转到Home_Fragment时,我的应用程序中出现了白色背景。请指导我为什么我在这里遇到这种问题我上传了我尝试过的代码。

加载首页片段代码

 private void loadHomeFragment() {
    selectNavMenu();
    setToolbarTitle();
    if (getSupportFragmentManager().findFragmentByTag(CURRENT_TAG) != null) {
        drawer.closeDrawers();
        return;
    }
    Runnable mPendingRunnable = new Runnable() {
        @Override
        public void run() {
            homeFragmentCalled();
        }
    };
    if (mPendingRunnable != null) {
        mHandler.post(mPendingRunnable);
    }
    drawer.closeDrawers();
    invalidateOptionsMenu();
}

导航视图

 private void setUpNavigationView() {
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.home:
                    navItemIndex = 0;
                    CURRENT_TAG = TAG_TIMETABLE;
                    homeFragmentCalled();
                    break;
                case R.id.nav_performance:
                    navItemIndex = 1;
                    CURRENT_TAG = TAG_PERFORMANCE;
                    secondFragmentCalled();
                    break;

                default:
                    navItemIndex = 0;
            }
            if (menuItem.isChecked()) {
                menuItem.setChecked(false);
            } else {
                menuItem.setChecked(true);
            }
            menuItem.setChecked(true);

            loadHomeFragment();

            return true;
        }
    });

    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.openDrawer, R.string.closeDrawer) {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
    };
    drawer.setDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();
}

调用片段方法导航

public void homeFragmentCalled() {
    Log.d("MainActivity ", "homeFragmentCalled called ");
    homeFragment = new HomeFragment();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
    fragmentTransaction.replace(R.id.frame, homeFragment, "homeFragment");
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

public void secondFragmentCalled() {
    performanceFragment = new PerformanceFragment();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
    fragmentTransaction.replace(R.id.frame, performanceFragment, "performanceFragment");
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

我谷歌并发现了很多问题。我跟着很多回答,但同样的错误是我使用的代码

replace 'add

更改了片段的背景颜色,也做了

fragmentTransaction.addToBackStack(null);

对不起我的英语也提前谢谢。

0 个答案:

没有答案