DrawerLayout和Navigation实现侧滑菜单

来源:互联网 发布:赵丽颖为什么能红 知乎 编辑:程序博客网 时间:2024/06/10 18:23

DrawerLayout
1.以android.support.v4.widget.DrawerLayout为根控件,导入:

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

2.Drawerlayout下包裹两个控件,第一个是内容控件,第二个是侧滑控件,使用android:layout_gravity来指定它的滑动位置,start表示左划出,end代表右划出

3.设置侧滑事件:mDrawerLayout.setDrawerListener(DrawerLayout.DrawerListener)


NavigationView
1.app:headerLayout="@layout/header_layout"表示引用一个头文件
2.app:menu="@menu/main"表示引用一个menu作为下面的点击项
3.获取头部:View.headerView=navigationView.getHeaderView(0)
4.item点击navigationView.setNavigationItemSelectedListener()

首先第一步定义侧滑菜单的头部布局:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <ImageView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:background="#AB594C"        android:src="@mipmap/bg_header" /></RelativeLayout>
第二步:定义侧滑菜单内容,在Menu下定义一个布局:

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">    <item        android:id="@+id/menu_app_update"        android:icon="@mipmap/ic_ref"        android:title="应用更新" />    <item        android:id="@+id/menu_message"        android:icon="@mipmap/ic_message"        android:title="消息中心" />    <item        android:id="@+id/menu_setting"        android:icon="@mipmap/ic_setting"        android:title="设置" /></menu>
第三步:定义一个主布局:

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/drawer_layout"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.yijia.yijia5play.MainActivity">    <!--内容控件-->    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/app_name" />    </LinearLayout>    <!--侧滑控件-->    <android.support.design.widget.NavigationView        android:id="@+id/navigation_view"        android:layout_width="280dp"        android:layout_height="match_parent"        android:layout_gravity="start"        app:headerLayout="@layout/layout_header"        app:menu="@menu/menu_left">    </android.support.design.widget.NavigationView></android.support.v4.widget.DrawerLayout>
第四步:DrawerLayout的点击事件

drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {    @Override    public void onDrawerSlide(View drawerView, float slideOffset) {        Log.e(TAG, "onDrawerSlide");    }    @Override    public void onDrawerOpened(View drawerView) {        Log.e(TAG, "onDrawerOpened");    }    @Override    public void onDrawerClosed(View drawerView) {        Log.e(TAG, "onDrawerClosed");    }    @Override    public void onDrawerStateChanged(int newState) {        Log.e(TAG, "onDrawerStateChanged");    }});headerView = navigationView.getHeaderView(0);//获取 头部headerView.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {        Toast.makeText(MainActivity.this, "点击了头部", Toast.LENGTH_LONG).show();    }});
第五步:获得侧滑菜单头部的view并设置点击事件

headerView = navigationView.getHeaderView(0);//获取 头部headerView.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {        Toast.makeText(MainActivity.this, "点击了头部", Toast.LENGTH_LONG).show();    }});
第六步:侧滑菜单内容的点击事件:

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {    @Override    public boolean onNavigationItemSelected(@NonNull MenuItem item) {        switch (item.getItemId()) {            case R.id.menu_app_update:                Toast.makeText(MainActivity.this, "点击了应用更新", Toast.LENGTH_LONG).show();                break;            case R.id.menu_message:                Toast.makeText(MainActivity.this, "点击了消息中心", Toast.LENGTH_LONG).show();                break;            case R.id.menu_setting:                Toast.makeText(MainActivity.this, "点击了设置", Toast.LENGTH_LONG).show();                break;        }        return false;    }});

原创粉丝点击