Android Support Design Library之NavigationView

此安卓支持库中需要讲解的第二个View就是NavigationView,相信大家在开发中经常会用到抽屉,那么谷歌也为大家提供了这个功能,并不需要去Github去下载使用开源的软件。NavigationView基本满足日常开发抽屉的所有要求,且效率也高。下面我们看下效果图后,将详细介绍其使用方式。



1.NavigationView与DrawerLayout的天作之合


NavigationView完整包名+类名如下:android.support.design.widget.NavigationView


而DrawerLayout完整包名+类名如:android.support.v4.widget.DrawerLayout


其两者搭配起来的XML布局文件如下:


<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:id="@+id/drawerlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:fitsSystemWindows="true">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"/>

        </android.support.design.widget.AppBarLayout>

        <LinearLayout
            android:id="@+id/linearlayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"/>

    </LinearLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/navigation"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer_menu"/>



</android.support.v4.widget.DrawerLayout>


属性解释如下:


①android:fitsSystemWindows="true":此属性在标题栏与抽屉中都设置了,保证系统状态栏与抽屉和标题一体化,因为我的测试手机是小米IS,Andorid 4.1的系统,该功能在5.0以上的手机才能显示其效果,故此测试图没有效果。


②app:headerLayout="@layout/drawer_header"也就是下图的区域:



这里在一般APP中都放的头像,我们这里只是做介绍,只要要drawer_header写你的任意布局,那么在此区域就会显示你所需要的界面。


③app:menu="@menu/drawer_menu":也就是下图所示区域:



也就是抽屉中的各种菜单选项。


2.实现抽屉中的菜单选项


在res/menu文件下新建drawer_menu.xml文件,写入如下代码:


<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/lyj_menu_datou"
            android:icon="@drawable/blog_tag_parent_expert"
            android:checked="true"
            android:title="我是大头"/>
        <item
            android:id="@+id/lyj_menu_xiaotou"
            android:icon="@drawable/blog_tag_parent_honorary_expert"
            android:title="我是小头"/>
        <item
            android:id="@+id/lyj_menu_chilun"
            android:icon="@drawable/blog_tag_parent_system_maintenance"
            android:title="我是齿轮"/>
    </group>
    <item android:title="其他">
        <menu>
            <item
                android:icon="@drawable/blog_tag_parent_cloud_computing"
                android:title="我是云盘"/>
            <item
                android:icon="@drawable/blog_tag_parent_comprehensive"
                android:title="我是标签"/>
        </menu>
    </item>
</menu>


属性解释如下:


①android:checkableBehavior="single":代表group所有菜单中,只能一次选择一个。


②android:checked="true":默认选中菜单项,此处为“我是大头”


③<item android:title="其他">:通过子菜单的形式,可以显示分割线与子标题


3.关联抽屉与标题栏


在继承自AppCompatActivity的Activity中,将标题栏设置显示为如下方式:


Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
toolbar.setTitle("我是大头");
setSupportActionBar(toolbar);


那么将抽屉与标题栏关联要用到ActionBarDrawerToggle。其构造方法有五个参数,按顺序依次如下:


㈠上下文

㈡DrawerLayout

㈢toolbar

㈣打开标识

㈤关闭标识


四与五不显示,仅做标识用。关联抽屉与标题栏代码如下:


DrawerLayout drawerLayout=(DrawerLayout)findViewById(R.id.drawerlayout);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
                R.string.drawer_open, R.string.drawer_close);
mDrawerToggle.syncState();//初始化状态
drawerLayout.setDrawerListener(mDrawerToggle);


使用setDrawerListener方式设置监听开关的打开与关闭。这样就实现了当按下菜单栏左侧按钮的时候,抽屉会打开。


4.设置NavigationView菜单选项的监听事件


该View给我们提供了菜单的监听回调方法:


setNavigationItemSelectedListener其需要实现的接口如下:


public interface OnNavigationItemSelectedListener {
        boolean onNavigationItemSelected(MenuItem var1);
    }


接口方法就一个,且参数就是菜单。下面我将实现其监听:


navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                switch (menuItem.getItemId()){//获取菜单itemID
                    case R.id.lyj_menu_datou:
                        getSupportFragmentManager().beginTransaction().replace(R.id.linearlayout,new DrawerFragment("我是大头")).commit();
                        toolbar.setTitle("我是大头");
                        break;
                    case R.id.lyj_menu_xiaotou:
                        getSupportFragmentManager().beginTransaction().replace(R.id.linearlayout,new DrawerFragment("我是小头")).commit();
                        toolbar.setTitle("我是小头");
                        break;
                    case R.id.lyj_menu_chilun:
                        getSupportFragmentManager().beginTransaction().replace(R.id.linearlayout,new DrawerFragment("我是齿轮")).commit();
                        toolbar.setTitle("我是齿轮");
                        break;
                    default:
                        break;
                }
                menuItem.setChecked(true);//设置菜单选中
                drawerLayout.closeDrawers();//当选中菜单后,就要关闭抽屉
                return false;
            }
        });


根据菜单ID,设置fragment界面。最后效果图,就是博文开始的图片。


5.导入支持库


昨天讲解忘记了,需要配置如下信息,方能使用控件:


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:design:23.0.1'
    compile 'com.android.support:recyclerview-v7:23.0.1'
    compile 'com.android.support:cardview-v7:23.0.1'
}


红色标记为导入的代码。


发布了94 篇原创文章 · 获赞 115 · 访问量 75万+

猜你喜欢

转载自blog.csdn.net/liyuanjinglyj/article/details/50434215
今日推荐