安卓Design之NavigationView的使用

前面讲解了Design包下的TabLayout的使用,下面将带来NavagationView和DrawLayout以及toolbar的联动。

项目已经同步至:https://github.com/nanchen2251/designNavigation-and-tabLayout

NavigationView 通过提供抽屉导航所需的框架让实现更简单,同时它还能够直接通过菜单资源文件直接生成导航元素。把NavigationView作为DrawerLayout的内容视图来使用。NavigationView处理好了和状态栏的关系,可以确保NavigationView在API21+设备上正确的和状态栏交互。

上运行图:

安卓Design之NavigationView的使用_第1张图片

 

以下代码在前面代码的基础上修改。

MainActivity.java

package com.example.nanchen.designtablayoutdemo;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {


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

        ViewPager vp = (ViewPager) findViewById(R.id.main_vp);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.main_tab_layout);

        Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.main_drawerlayout);

        //设置Toolbar和DrawerLayout实现动画和联动
        this.setSupportActionBar(toolbar);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,0,0);
        drawerLayout.addDrawerListener(toggle);//设置监听
        toggle.syncState();//加上同步
        getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
        List list = new ArrayList<>();
        for (int i = 1; i < 5; i++) {
//            list.add(String.format(Locale.CHINA,"第02d%页",i));
            list.add("第"+i+"页");
        }
        vp.setAdapter(new MyAdapter(getSupportFragmentManager(),list));

        tabLayout.setupWithViewPager(vp);

        NavigationView navigationView = (NavigationView) findViewById(R.id.main_navigation);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_item1:
                Toast.makeText(this,"你点击了菜单1",Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu_item2:
                Toast.makeText(this,"你点击了菜单2",Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu_item3:
                Toast.makeText(this,"你点击了菜单3",Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu_item4:
                Toast.makeText(this,"你点击了菜单4",Toast.LENGTH_SHORT).show();
                break;
        }
        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }
}

主布局




    
    
    
   

       

           

           
       

       

       
   



  

 

 

 navigation.xml




    
        
        
        
    


    
    
    
    


  

header.xml




    

    

  

header.bg.xml




    

  

navigation_selector.xml

 



    
    

  

你可能感兴趣的:(安卓Design之NavigationView的使用)