搞定 Material Design 相关控件5:NavigationView

1 开始使用

compile 'com.android.support:design:25.3.1'

2 滑动菜单不覆盖Toolbar

 

    

    
        
        

            
                
        

        
        

    

  • layout_navigation_header_layout.xml



 

 

  • id_menu_navigation_view.xml



    
        
        
        
        
    

    
        
            
            
        
    

3 配合Toolbar左侧按钮

  • 设置
  DrawerLayout drawer = (DrawerLayout) findViewById(R.id.id_drawer_layout);
        //左侧按钮 旋转
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer,
                toolbar,
                R.string.openDrawerContentDesc, R.string.closeDrawerContentDesc);
        drawer.addDrawerListener(toggle);
        toggle.syncState();//同步状态
  • 不设置
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer,
                R.string.openDrawerContentDesc, R.string.closeDrawerContentDesc);

4 滑动菜单覆盖Toolbar




   
   
            
                
        

    


5 监听菜单点击

 nv_menu_left.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_item1:
                        mToolBar.setTitle("选项一");
                        break;
                    case R.id.navigation_item2:
                        mToolBar.setTitle("选项二");
                        break;
                    case R.id.navigation_item3:
                        mToolBar.setTitle("选项三");
                        break;
                    case R.id.navigation_sub_item1:
                        mToolBar.setTitle("简介");
                        break;
                    case R.id.navigation_sub_item2:
                        mToolBar.setTitle("详情");
                        break;
                    case R.id.navigation_sub_item3:
                        mToolBar.setTitle("更多");
                        break;
                }

               //选中的条件是该menu的checkable为true
                item.setCheckable(true);
                //将选中设为文字选中的状态
                //item.setChecked(true);
                //
                //id_navigation_view.setCheckedItem(item.getItemId());
                //关闭抽屉
                mDrawerLayout.closeDrawers();
                return true;
            }
        });

6 菜单样式相关

  • 让菜单图片显示原来的颜色
app:itemIconTint=""
//或者
mNavigationView.setItemIconTintList(null);
  • 菜单过多,隐藏滚动条
NavigationMenuView menuView  = (NavigationMenuView) mNavigationView.getChildAt(0);
menuView.setVerticalScrollBarEnabled(false);
  • 样式自定义
菜单item背景
 app:itemBackground
 
菜单item文字颜色
app:itemTextColor

文字样式
app:itemTextAppearance
  • 默认选中菜单

menu xml 设置

android:checked="true"
  • 默认选中菜单(带背景)
    方法1
    menu xml 设置
android:checkable="true"

代码中 设置

 //设置默认选中 选中的条件是该menu的checkable为true
         id_navigation_view.setCheckedItem(R.id.id_menu_item_patient_list);

方法2
代码中 设置

        //设置默认选中
        if (id_navigation_view.getMenu()!=null&&id_navigation_view.getMenu().size()>0){
            mMenuItem=id_navigation_view.getMenu().getItem(0);
            mMenuItem.setCheckable(true);
            //选中的条件是该menu的checkable为true
            id_navigation_view.setCheckedItem(mMenuItem.getItemId());
        }
  • 动态隐藏菜单
NavigationView.getMenu().findItem(R.id.item_1);
item.setVisible(false);//false即隐藏,true即显示

7 获取head的view

 headerView = mNavigationView.getHeaderView(0);

8 监听侧滑

  • onDrawerSlide方法中的slideOffset,则对应于DrawerLayout展开的偏移值,全部展开时为1,全部收起时为0,onDrawerStateChanged对应于所处的状态:STATE_IDLE/STATE_DRAGGING/STATE_SETTLING
  mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                Log.d("mDrawerLayout", "onDrawerSlide, slideOffset=" + slideOffset);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                Log.d("mDrawerLayout", "onDrawerOpened");
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                Log.d("mDrawerLayout", "onDrawerClosed");
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                Log.d("mDrawerLayout", "onDrawerStateChanged, state=" + newState);

            }
        });

9 参考文档

  • Android Design Support Library系列之二:NavigationView的使用 http://www.jianshu.com/p/ddd23992b3ed
  • Material Design 控件知识梳理(5) - DrawerLayout && NavigationView http://www.jianshu.com/p/d70cfd724c7f

你可能感兴趣的:(搞定 Material Design 相关控件5:NavigationView)