android.support.design.widget.NavigationView上的getFragmentManager

时间:2015-05-30 20:37:47

标签: android navigation-drawer android-support-library android-design-library

在新的支持设计库中,我无法替换当前片段,请查看下面的代码。虽然Toasts正在工作并在屏幕上显示。每当我要点击导航抽屉上的任何项目时,我都会出现空白屏幕

private void setupDrawerContent(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.navigation_home:
                    getFragmentManager().beginTransaction().replace(R.id.container, new HomeFragment()).commit();
                    Toast toast;
                    toast = Toast.makeText(getApplicationContext(), "Home", Toast.LENGTH_LONG);
                    toast.show();

                case R.id.navigation_settings:
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        getFragmentManager().beginTransaction().replace(R.id.container, new SettingsFragmentLollipop()).commit();
                    } else {
                        getFragmentManager().beginTransaction().replace(R.id.container, new SettingsFragment()).commit();
                    }
                    Toast toast1;
                    toast1 = Toast.makeText(getApplicationContext(), "Settings", Toast.LENGTH_LONG);
                    toast1.show();
            }
            menuItem.setChecked(true);
            mDrawerLayout.closeDrawers();
            return true;
        }
    });
}

2 个答案:

答案 0 :(得分:1)

好的,我让它运转起来,经过几个小时的追踪跟踪我的代码有什么问题,我觉得很蠢。 在我的xml中,我使用android.support.design.widget.CoordinatorLayout作为Fragment容器,我在其中用FrameLayout替换它。我在文档中做到了这一点,它说CoordinatorLayout只是一个超级FrameLayout ,但我想它不会作为Fragments的容器工作。我现在处理重叠片段。

答案 1 :(得分:0)

尝试使用类AppCompatActivity

.logo {
    max-height: 0px;
    background-color: #FC3;
    -webkit-transition: max-height 1s linear, opacity 1s linear;        
       -moz-transition: max-height 1s linear, opacity 1s linear;
         -o-transition: max-height 1s linear, opacity 1s linear;
            transition: max-height 1s linear, opacity 1s linear;
    visibility: hidden;
    opacity: 0;
}   

@media screen and (min-width: 500px){
    .logo {
        max-height: 200px;
        visibility: visible;
        opacity: 1;
        -webkit-transition: max-height 1s linear, opacity 1s linear;
           -moz-transition: max-height 1s linear, opacity 1s linear;
             -o-transition: max-height 1s linear, opacity 1s linear;
                transition: max-height 1s linear, opacity 1s linear;
    }   
}

我自己只是在玩新图书馆