替换每个导航抽屉片段

时间:2015-08-26 13:15:20

标签: android android-fragments android-toolbar android-collapsingtoolbarlayout

我遇到了Android的新材料设计支持库的这个所谓的Collapsing Toolbar,我正在考虑将这个新设计应用到我的一些片段中。

所以我也碰巧到达了这个第三方库Material View Pager,它与Collapsing Toolbar非常相似,只不过它有一个内置的View Pager。我想将它集成到我的应用程序中,因为它的功能非常适合某个片段。由于它与Collapsing Toolbar类似,因此它提供了自己的Toolbar,需要通过setSupportActionBar()进行设置。

接下来,从下面的代码开始,我有一个Navigation Drawer负责导航我的五个片段之间的导航(COR,班级时间表,评估,INC监视器和清除)

现在,我想将此Collapsing Toolbar应用于我的两个片段(COR& Clearance),并将此Material View Pager应用于一个片段(评估)。问题是,我已经为我的所有片段设置了标准Toolbar

toolbar.xml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ActionBarPopupThemeOverlay"
    app:menu="@menu/menu_toolbar"
    android:id="@+id/toolbar"
    android:background="@color/primary_dark"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:elevation= "4dp" />

MainActivity.java

public void setupToolbar() {
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
    }
}

public void setupNavigationDrawer() {
    drawer_layout = (DrawerLayout) findViewById(R.id.drawer_layout);
    navigation_view = (NavigationView) findViewById(R.id.navigation_view);

    navigation_view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            fragment_manager = getSupportFragmentManager();
            fragment_transaction = fragment_manager.beginTransaction();
            fragment_transaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);

            menuItem.setChecked(true);

            switch (menuItem.getItemId()) {
                case R.id.cor:   //Collapsing Toolbar here
                    fragment_transaction.replace(R.id.content, cor_fragment);
                    break;
                case R.id.sched: //Normal Toolbar here
                    fragment_transaction.replace(R.id.content, sched_fragment);
                    break;
                case R.id.eval:  //Material View Pager Toolbar here
                    fragment_transaction.replace(R.id.content, eval_fragment);
                    break;
                case R.id.inc:   //Normal Toolbar here
                    fragment_transaction.replace(R.id.content, inc_fragment);
                    break;
                case R.id.clear: //Collapsing Toolbar here
                    fragment_transaction.replace(R.id.content, clear_fragment);
                    break;
            }

            mHandler = new Handler();
            mHandler.removeCallbacksAndMessages(null);
            int delay = 400;
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    fragment_transaction.addToBackStack(null);
                    fragment_transaction.commit();
                }
            }, delay);

            drawer_layout.closeDrawers();

            return true;
        }
    });

    drawer_toggle = new ActionBarDrawerToggle(this, drawer_layout, toolbar, R.string.open_drawer, R.string.close_drawer) {
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }
    };
    drawer_layout.setDrawerListener(drawer_toggle);
    drawer_toggle.syncState();

    drawer_layout.openDrawer(GravityCompat.START);
}

从上面的注释中可以看出,对于每个正在加载的片段,我想加载一个不同的工具栏。但在我写的代码中,我只能setSupportActionBar()一个Toolbar。我尝试在setSupportActionBar(toolbar_of_choice)语句的每个case中添加switch,但它会崩溃该应用。

Navigation Drawer中的每个片段设置不同的工具栏是否有任何变通方法?

1 个答案:

答案 0 :(得分:0)

@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
    fragment_manager = getSupportFragmentManager();
    fragment_transaction = fragment_manager.beginTransaction();
    fragment_transaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);

    menuItem.setChecked(true);

    switch (menuItem.getItemId()) {
        case R.id.cor:   //Collapsing Toolbar here
            fragment_transaction.replace(R.id.content, cor_fragment);
            Toolbar toolbar =(Toolbar)findViewById(R.id.toolbar);
            toolbar.setVisibility(View.GONE);
            // use can replace original toolbar with collapsing//
            break;
        case R.id.sched: //Normal Toolbar here
            fragment_transaction.replace(R.id.content, sched_fragment);
Toolbar toolbar =(Toolbar)findViewById(R.id.toolbarmain);
        toolbar.setVisibility(View.VISIBLE);
            break;
        case R.id.eval:  //Material View Pager Toolbar here
            fragment_transaction.replace(R.id.content, eval_fragment);
            break;
        case R.id.inc:   //Normal Toolbar here
            fragment_transaction.replace(R.id.content, inc_fragment);
Toolbar toolbar =(Toolbar)findViewById(R.id.toolbarmain);
        toolbar.setVisibility(View.VISIBLE);
            break;
        case R.id.clear: //Collapsing Toolbar here
            fragment_transaction.replace(R.id.content, clear_fragment);
            break;
    }

使用上面的代码,您可以轻松利用折叠布局。