Android M 新控件 TabLayout 與 NavigationView 實踐

項目地址

DrawerSample.gif

核心是使用 DrawerLayout + NavigationView 實現側滑菜單
使用 TabLayout + ViewPager 實現Tab效果

NavigationView 是 M 的新控件,可以輕松編寫靜態的菜單,看它提供的api中貌似不能動態添加菜單項
TabLayout 也是作為 M 的新控件之一,其實在此之前已經有很多大神寫出很好用的 TabIndicator 了,只是這次官方統一了一下,不需要使用第三方庫。

依賴庫

    //核心
    compile 'com.android.support:design:23.0.1'
    //Circle View
    compile 'de.hdodenhof:circleimageview:1.3.0'
    compile 'com.android.support:recyclerview-v7:23.0.1'

circleimageview只是在drawer_header.xml 中作為顯示頭像之用,不想用可以刪除之

側滑菜單的布局

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"
    >

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        />
    <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/drawer_header"
        android:fitsSystemWindows="true"
        app:menu="@menu/drawer"/>
</android.support.v4.widget.DrawerLayout>

菜單項的顯示

對于菜單項的顯示,并不需要重新寫一個布局,只需要在 menu 文件夾下建立一個菜單項文件,這應該是 NavigationView 最大的優點了,懶癌晚期的我終于有救了。
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:checked="true"
            android:id="@+id/menu_item_1"
            android:title="@string/item1"
            />
        <item
            android:id="@+id/menu_item_2"
            android:title="@string/item2"
            />
        <item
            android:id="@+id/menu_item_3"
            android:title="@string/item3"
            />

        <item
            android:id="@+id/menu_item_4"
            android:title="@string/item4"
            />

        <item
            android:id="@+id/menu_item_5"
            android:title="@string/item5"
            />

    </group>
</menu>

顯示Tab

Tab顯示部分用到了 ViewPagerTabLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/toolbar_tabbar"
            android:background="?attr/colorPrimary"
            android:scrollbars="horizontal"
            app:tabTextColor="@android:color/black"
            app:tabSelectedTextColor="?attr/colorAccent"
            app:tabBackground="@android:color/white"
            app:tabIndicatorColor="?attr/colorAccent"
            app:tabPaddingStart="8dp"
            app:tabPaddingEnd="8dp"
            app:tabMode="scrollable"
            app:tabGravity="center"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />


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

TabLayout 常用的屬性有三個:

屬性 含義
app:tabSelectedTextColor Tab 被選中字體的顏色
app:tabTextColor Tab未被選中字體的顏色
app:tabIndicatorColor Tab指示器下標的顏色

還有兩個比較重要的屬性
app:tabMode

  • fix:表示所有tab會擠在屏幕內,不可滾動|
  • scrollable:表示Tab可以滾動

app:tabGravity

  • center:點擊接近屏幕邊緣的Tab時會聚焦(當然會聚焦啦)并居中
  • fill:點擊接近屏幕邊緣的Tab時會向屏幕中心移動,直至能完全顯示整個Tab標簽,不會居中

建立 ViewPagerTab 的聯系

mTabLayout.setupWithViewPager(mViewPager);

實現菜單與Tab聯動

  • 點擊菜單,Tab滾動
//設置菜單點擊監聽
 mNavigationview.setNavigationItemSelectedListener(this);

  • Tab滾動,更改菜單選中項目
 //設置ViewPager滾動監聽
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, 
                int positionOffsetPixels) {
                
       }

       @Override
       public void onPageSelected(int position) {
           //更改頁面后,通知Activity更改菜單選中項
           ((MainActivity)getActivity()).setMenuItem(position);
       }
       
       @Override
       public void onPageScrollStateChanged(int state) {
           }
    });
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容