Android导航抽屉设置标题

时间:2016-02-12 10:35:57

标签: android navigation drawer

当用户点击导航抽屉的特定片段时,我想更改导航抽屉的标题。我是Android编程世界的新手。所以请帮助以下是我的代码

主要活动代码

    package com.androidbelieve.HIT_APP;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity{
    DrawerLayout mDrawerLayout;
    NavigationView mNavigationView;
    FragmentManager mFragmentManager;
    FragmentTransaction mFragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /**
         *Setup the DrawerLayout and NavigationView
         */

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;




        /**
         * Lets inflate the very first fragment
         * Here , we are inflating the TabFragment as the first Fragment
         */

        mFragmentManager = getSupportFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();
        // mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
        mFragmentTransaction.replace(R.id.containerView,new HomeFragment()).commit();


        /**
         * Setup click events on the Navigation View Items.
         */

        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                mDrawerLayout.closeDrawers();




              if (menuItem.getItemId() == R.id.nav_item_home) {
                    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.containerView,new HomeFragment()).commit();

                }

                if (menuItem.getItemId() == R.id.nav_item_dept) {
                    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.containerView,new DeptFragment()).commit();

                }

                if (menuItem.getItemId() == R.id.nav_item_facility) {
                    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.containerView,new FacilityFragment()).commit();

                }

                if (menuItem.getItemId() == R.id.nav_item_placement) {
                    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.containerView,new PlacementFragment()).commit();

                }

                if (menuItem.getItemId() == R.id.nav_item_gallery) {
                    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.containerView,new GalleryFragment()).commit();

                }

                if (menuItem.getItemId() == R.id.nav_item_developer) {
                    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.containerView,new DeveloperFragment()).commit();

                }



                if (menuItem.getItemId() == R.id.nav_item_near) {
                    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.containerView,new NearFragment()).commit();

                }


                if (menuItem.getItemId() == R.id.nav_item_notify) {
                    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.containerView,new NotifyFragment()).commit();

                }

                if (menuItem.getItemId() == R.id.nav_item_vtu) {
                    FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                    xfragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
                }


                return false;

            }



        });

        /**
         * Setup Drawer Toggle of the Toolbar
         */

        android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
        ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
                R.string.app_name);

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        mDrawerToggle.syncState();

    }
}

当一个人选择任何抽屉物品时,我认为有些事情需要在这里完成。

需要的任何其他代码让我知道

1 个答案:

答案 0 :(得分:1)

在活动的android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);方法setContentView之后移动onCreate以上的变量。把它final

当用户在ToolBar中选择onNavigationItemSelected的导航抽屉设置标题时,如下所示。

if (menuItem.getItemId() == R.id.nav_item_home) {
   FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
   fragmentTransaction.replace(R.id.containerView,new HomeFragment()).commit();
   toolbar.setTitle("Your Fragment Name");
   }

设置ICON,你可以使用

toolbar.setLogo(ContextCompat.getDrawable(MainActivity.this, R.drawable.cheese_1));