Material Design之DrawerLayout與NavigationView實現抽屜效果

記錄自己對Material Design開發

之前項目引用的是SlidingMenu框架的實現,然後決定自己試試DrawerLayout與NavigationView的實現。在嘗試之後,期間也就遇到了一個問題,發現實現起來非常的簡單,網上的教程一大把,基本所有的問題都可以解決。

基本佈局情況,就是用DrawerLayout作爲最外層,嵌套主界面內容佈局,還有NavigationView。
這裏寫圖片描述
這裏寫圖片描述

主界面的佈局activity_main.xml

<?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"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    <!--內容區-->
    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/main_content_all"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            android:id="@+id/appbar"
            layout="@layout/main_toolbar" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">


            <FrameLayout
                android:id="@+id/main_content"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

            </FrameLayout>

            <View style="@style/line_horizontal" />

            <view
                android:id="@+id/main_tabhost"
                class="android.support.v4.app.FragmentTabHost"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom" />
        </LinearLayout>
    </android.support.design.widget.CoordinatorLayout>

    <!--左側導航菜單-->
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_header"
        app:menu="@menu/main_drawer" />

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



main_toolbar.xml佈局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/main_toolbar"
        android:layout_width="match_parent"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:layout_height="wrap_content"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.ActionBar">

    </android.support.v7.widget.Toolbar>

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

navigation_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:gravity="center"
    android:background="@color/colorPrimaryDark"
    android:layout_height="200dp">

    <ImageView
        android:id="@+id/imageView"
        android:scaleType="fitXY"
        android:contentDescription="jdalj"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher" />
</LinearLayout>

main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/navigation_item_home"
            android:icon="@mipmap/ic_launcher"
            android:title="首頁" />
        <item
            android:id="@+id/navigation_item_blog"
            android:icon="@mipmap/ic_launcher"
            android:title="博客" />
        <item
            android:id="@+id/navigation_item_about"
            android:icon="@mipmap/ic_launcher"
            android:title="關於" />
    </group>

</menu>

代碼實現部分

toolbar

	//toolbar實現部分
    Toolbar toolbar= ((Toolbar) findViewById(R.id.main_toolbar));
    toolbar.setTitle("Kepler");
    //替換actionbar
    setSupportActionBar(toolbar);

navigationView

final NavigationView navigationView= ((NavigationView) findViewById(R.id.navigation_view));
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                VtToast.s(mContext,"--"+item.getItemId());
                drawerLayout.closeDrawer(navigationView);
                return false;
            }
        });

drawerLayout

drawerLayout = ((DrawerLayout) findViewById(R.id.drawer_layout));

        ActionBarDrawerToggle actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);
        actionBarDrawerToggle.syncState();
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                VtLog.i("drawer--slide"+slideOffset);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                VtLog.i("drawer--open"+"");
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                VtLog.i("drawer--close"+"");
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                VtLog.i("drawer--changed"+newState);
            }
        });

注意:

查看DrawerLayout源碼,其中closeDrawer(View drawerView),openDrawer(View drawerView),這裏的參數drawerView指的是可以進行抽拉的視圖,在這裏也就是特別指NavigationView對象,所以在傳參的時候直接傳入NavigationView對象就行了。

如果想仔細查看所有代碼的話,可以直接導下我的項目自己運行,並測試。

項目地址:https://github.com/voctex/Kepler

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章