Android Studio Navigation

Navigation

##Navigation:列表组件
用法:导依赖
添加头布局:headerlayout:“头布局文件”
添加列表:新建Menu文件
设置title;
设置icon;
这些放在group里面
布局中添加列表:Menu=“列表文件”

Activity中:
getHeaderView(布局id)/findbyid();获得头文件的控件
setNavigationLinstener():列表布局的监听

代码

布局文件

<android.support.v4.widget.DrawerLayout
        android:id="@+id/main_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.NavigationView
            android:id="@+id/main_navi"
            android:layout_width="400dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            app:menu="@menu/navimenu"
            app:headerLayout="@layout/headerview"
            >android.support.design.widget.NavigationView>

       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent">
          <android.support.v7.widget.RecyclerView
              android:id="@+id/main_list"
              android:layout_width="match_parent"
              android:layout_height="match_parent">android.support.v7.widget.RecyclerView>
       LinearLayout>
    android.support.v4.widget.DrawerLayout>

menu文件

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/group" android:checkableBehavior="none">
    <item android:id="@+id/item1" android:title="Toolbar" android:icon="@drawable/qb"/>
    <item android:id="@+id/item2" android:title="GradView" android:icon="@drawable/sc"/>
    <item android:id="@+id/item3" android:title="Pattele" android:icon="@drawable/wjj"/>
    <item android:id="@+id/item4" android:title="仿稀土" android:icon="@drawable/xht"/>
    <item android:id="@+id/item5" android:title="主体style" android:icon="@drawable/cx"/>
    <item android:id="@+id/item6" android:title="模式切换" android:icon="@drawable/qb"/>
group>
menu>

activity文件

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar=findViewById(R.id.main_tool);
        drawerLayout=findViewById(R.id.main_drawer);
        navigationView=findViewById(R.id.main_navig);


        setSupportActionBar(toolbar);//让他来替换actionbar

        //给导航按钮添加点击事件
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"你点击了导航按钮",Toast.LENGTH_SHORT).show();
            }
        });


        //绑定Toolbar和drawerLayout
        ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close);
        toggle.syncState();
        drawerLayout.addDrawerListener(toggle);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id){
            case R.id.menu1:
                Toast.makeText(MainActivity.this,"你点击了菜单一",Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu1_1:
                Toast.makeText(MainActivity.this,"你点击了子菜单一",Toast.LENGTH_SHORT).show();
                if(drawerLayout.isDrawerOpen(Gravity.LEFT)){
                    drawerLayout.closeDrawer(Gravity.LEFT);
                }else{
                    drawerLayout.openDrawer(Gravity.LEFT);
                }
                break;
            case R.id.menu1_2:
                Toast.makeText(MainActivity.this,"你点击了子菜单二",Toast.LENGTH_SHORT).show();
                if(drawerLayout.isDrawerOpen(Gravity.LEFT)){
                    drawerLayout.closeDrawer(Gravity.LEFT);
                }else{
                    drawerLayout.openDrawer(Gravity.LEFT);
                }
                break;
            case R.id.menu1_3:
                if(drawerLayout.isDrawerOpen(Gravity.LEFT)){
                    drawerLayout.closeDrawer(Gravity.LEFT);
                }else{
                    drawerLayout.openDrawer(Gravity.LEFT);
                }
                Toast.makeText(MainActivity.this,"你点击了子菜单三",Toast.LENGTH_SHORT).show();
                break;

        }

        return super.onOptionsItemSelected(item);
    }

你可能感兴趣的:(Android Studio Navigation)