Android M 新控件 TabLayout 與 NavigationView 實踐

項目地址

DrawerSample.gif

核心是使用 DrawerLayout + NavigationView 實現(xiàn)側(cè)滑菜單
使用 TabLayout + ViewPager 實現(xiàn)Tab效果

NavigationView 是 M 的新控件谷朝,可以輕松編寫靜態(tài)的菜單缤底,看它提供的api中貌似不能動態(tài)添加菜單項
TabLayout 也是作為 M 的新控件之一易稠,其實在此之前已經(jīng)有很多大神寫出很好用的 TabIndicator 了夹界,只是這次官方統(tǒng)一了一下,不需要使用第三方庫引谜。

依賴庫

    //核心
    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 中作為顯示頭像之用锦积,不想用可以刪除之

側(cè)滑菜單的布局

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 最大的優(yōu)點了,懶癌晚期的我終于有救了榜贴。
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會擠在屏幕內(nèi)豌研,不可滾動|
  • scrollable:表示Tab可以滾動

app:tabGravity

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

建立 ViewPagerTab 的聯(lián)系

mTabLayout.setupWithViewPager(mViewPager);

實現(xiàn)菜單與Tab聯(lián)動

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

  • Tab滾動,更改菜單選中項目
 //設置ViewPager滾動監(jiān)聽
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) {
           }
    });
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末霜浴,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子蓝纲,更是在濱河造成了極大的恐慌阴孟,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,084評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件税迷,死亡現(xiàn)場離奇詭異永丝,居然都是意外死亡,警方通過查閱死者的電腦和手機箭养,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評論 3 392
  • 文/潘曉璐 我一進店門慕嚷,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人毕泌,你說我怎么就攤上這事喝检。” “怎么了撼泛?”我有些...
    開封第一講書人閱讀 163,450評論 0 353
  • 文/不壞的土叔 我叫張陵蛇耀,是天一觀的道長。 經(jīng)常有香客問我坎弯,道長纺涤,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,322評論 1 293
  • 正文 為了忘掉前任抠忘,我火速辦了婚禮撩炊,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘崎脉。我一直安慰自己拧咳,他們只是感情好,可當我...
    茶點故事閱讀 67,370評論 6 390
  • 文/花漫 我一把揭開白布囚灼。 她就那樣靜靜地躺著骆膝,像睡著了一般祭衩。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上阅签,一...
    開封第一講書人閱讀 51,274評論 1 300
  • 那天掐暮,我揣著相機與錄音,去河邊找鬼政钟。 笑死路克,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的养交。 我是一名探鬼主播精算,決...
    沈念sama閱讀 40,126評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼碎连!你這毒婦竟也來了灰羽?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,980評論 0 275
  • 序言:老撾萬榮一對情侶失蹤鱼辙,失蹤者是張志新(化名)和其女友劉穎谦趣,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體座每,經(jīng)...
    沈念sama閱讀 45,414評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡前鹅,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,599評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了峭梳。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片舰绘。...
    茶點故事閱讀 39,773評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖葱椭,靈堂內(nèi)的尸體忽然破棺而出捂寿,到底是詐尸還是另有隱情,我是刑警寧澤孵运,帶...
    沈念sama閱讀 35,470評論 5 344
  • 正文 年R本政府宣布秦陋,位于F島的核電站,受9級特大地震影響治笨,放射性物質(zhì)發(fā)生泄漏驳概。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,080評論 3 327
  • 文/蒙蒙 一旷赖、第九天 我趴在偏房一處隱蔽的房頂上張望顺又。 院中可真熱鬧,春花似錦等孵、人聲如沸稚照。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽果录。三九已至上枕,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間弱恒,已是汗流浹背辨萍。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留斤彼,地道東北人分瘦。 一個月前我還...
    沈念sama閱讀 47,865評論 2 370
  • 正文 我出身青樓蘸泻,卻偏偏與公主長得像琉苇,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子悦施,可洞房花燭夜當晚...
    茶點故事閱讀 44,689評論 2 354

推薦閱讀更多精彩內(nèi)容