DrawerLayout和Navigation实现侧滑菜单

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;
    }
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值