Android 資訊類App項目實戰(zhàn) 第一章 滑動頂部導(dǎo)航欄

前言:

正在做一個資訊類app,打算一邊做一邊整理镇防,供自己學(xué)習(xí)與鞏固。用到的知識復(fù)雜度不高憨栽,僅適于新手。經(jīng)驗不多翼虫,如果寫出來的代碼有不好的地方歡迎討論。

該系列的其他文章
第二章 retrofit獲取網(wǎng)絡(luò)數(shù)據(jù)

第一章 滑動頂部導(dǎo)航欄

本章內(nèi)容最終效果:

image

知識點:ViewPager,TabLayout,FragmentPagerAdapter,Fragment

學(xué)習(xí)目標(biāo):

1屡萤、掌握ViewPager珍剑、TabLayout的結(jié)合使用。

2死陆、運(yùn)用TabLayout與ViewPager招拙、FragmentPagerAdapter的相關(guān)知識實現(xiàn)可滑動的頂部導(dǎo)航欄唧瘾。

可滑動的頂部導(dǎo)航欄在國內(nèi)的很多app中經(jīng)常出現(xiàn)(最典型的例子:網(wǎng)易云音樂),使用者只需左右滑動屏幕即可切換到自己想要的頁面别凤,非常方便饰序。本章內(nèi)容將仿照網(wǎng)易云音樂ui來實現(xiàn)頂部導(dǎo)航欄的效果。

項目實戰(zhàn):

本章用到的drawable資源规哪、values資源皆存放在百度網(wǎng)盤

(請將values文件夾中的style.xml或color.xml更新一致后再運(yùn)行求豫,如有后續(xù)更新自行修改)

1.1 主頁面布局

主頁面布局代碼不多,僅僅是include了主體布局main_content诉稍。

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.idlereader.MainActivity">

<include layout="@layout/main_content" />

</LinearLayout>

新建布局文件main_content.xml蝠嘉,代碼:

main_content.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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

<View
    android:id="@+id/view_status"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:visibility="gone"></View>

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbars"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorTheme"
    app:contentInsetStart="0dp"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/iv_title_news"
                android:layout_width="55dp"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:src="@drawable/titlebar_news" />

            <ImageView
                android:id="@+id/iv_title_movie"
                android:layout_width="55dp"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:padding="13dp"
                android:src="@drawable/titlebar_movie" />

            <ImageView
                android:id="@+id/iv_title_video"
                android:layout_width="55dp"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:src="@drawable/titlebar_video" />
        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.Toolbar>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/vp_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="blocksDescendants" />
</FrameLayout>

</LinearLayout>

在該布局中,我們使用到了Toolbar和ViewPager,ViewPager的作用是存放fragment杯巨,Toolbar做為頂部導(dǎo)航欄蚤告。

1.2 主頁面邏輯代碼

在寫主頁面代碼前,我們先建3個fragment服爷,用來放進(jìn)我們的FragmentPagerAdapter里杜恰。
新建3個Fragment,分別命名為FgNewsFragment、FgMovieFragment仍源、FgVideoFragment(這個命名方式是因為我習(xí)慣用第三方插件Code Generate生成fragment文件或activity文件)心褐。


3個fragment的布局.png

3個fragment.png

這3個Fragment的不需要寫什么東西,顯示一個TextView就好了


FgMovieFragment的布局代碼
FgMovieFragment的代碼镜会,其他fragment也這么寫就好了

有了這三個Fragment后檬寂,我們來給ViewPager寫一個Adapter]

新建java文件,命名為MyFragmentAdapter.java


MyFragmentAdapter.java
MyFragmentAdapter.java

寫完了適配器戳表,再來完善MainActivity的代碼:


MainActivity.java

完成后的效果:


頂部導(dǎo)航欄效果1.gif

2.1 新聞頁面導(dǎo)航欄

在新聞頁面導(dǎo)航欄要用到TabLayout桶至,這需要我們導(dǎo)一下庫:


build.gradle

首先還是布局,把上面創(chuàng)建的fg_news.xml的代碼修改一下匾旭。

fg_news.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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.design.widget.TabLayout
    android:id="@+id/tl_news"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@color/colorTheme"
    app:tabIndicatorColor="@color/colorWhite"
    app:tabSelectedTextColor="@color/colorWhite"
    app:tabTextColor="@color/colorTabTextNormal" />

<android.support.v4.view.ViewPager
    android:id="@+id/vp_news"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

加了個TabLayout镣屹,我把選擇狀態(tài)和自然狀態(tài)的設(shè)置都寫進(jìn)布局文件里了。

2.2 新聞頁面導(dǎo)航欄邏輯代碼

建一個Fragment,命名為FgNewsListFragment


FgNewsListFragment

fg_news_list

布局代碼也是只顯示個TextView

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

<TextView
    android:id="@+id/tv_news"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="top"
    android:textSize="100dp" />

</LinearLayout>

FgNewsListFragment的代碼:


FgNewsListFragment

接下來是修改FgNewsFragment的代碼:


FgNewsFragment

最后效果:


image

需要注意的是在主頁面adapter的實例化中价涝,F(xiàn)ragmentManager用的是getSupportFragmentManager()女蜈。

MyFragmentAdapter adapter = new MyFragmentAdapter(getSupportFragmentManager(),
mFragmentList);

而新聞頁面用的是getChildFragmentManager(),

MyFragmentAdapter adapter=new MyFragmentAdapter(getChildFragmentManager(),
            fragments,fragmentTitles);

這是因為新聞頁面里的fragment是嵌套于新聞fragment的,如果繼續(xù)用getSupportFragmentManager()的話色瘩,getFragmentManager到的是activity對所包含fragment的Manager伪窖,所以要使用getChildFragmentManager()。

以上就是實現(xiàn)頂部導(dǎo)航欄的所有內(nèi)容居兆。

下一章:
第二章 retrofit獲取網(wǎng)絡(luò)數(shù)據(jù)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末覆山,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子泥栖,更是在濱河造成了極大的恐慌簇宽,老刑警劉巖勋篓,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異魏割,居然都是意外死亡譬嚣,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進(jìn)店門钞它,熙熙樓的掌柜王于貴愁眉苦臉地迎上來拜银,“玉大人,你說我怎么就攤上這事须揣⊙喂桑” “怎么了?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵耻卡,是天一觀的道長疯汁。 經(jīng)常有香客問我,道長卵酪,這世上最難降的妖魔是什么幌蚊? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮溃卡,結(jié)果婚禮上溢豆,老公的妹妹穿的比我還像新娘。我一直安慰自己瘸羡,他們只是感情好漩仙,可當(dāng)我...
    茶點故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著犹赖,像睡著了一般队他。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上峻村,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天麸折,我揣著相機(jī)與錄音,去河邊找鬼粘昨。 笑死垢啼,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的张肾。 我是一名探鬼主播芭析,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼吞瞪!你這毒婦竟也來了放刨?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤尸饺,失蹤者是張志新(化名)和其女友劉穎进统,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體浪听,經(jīng)...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡螟碎,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了迹栓。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片掉分。...
    茶點故事閱讀 39,932評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖克伊,靈堂內(nèi)的尸體忽然破棺而出酥郭,到底是詐尸還是另有隱情,我是刑警寧澤愿吹,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布不从,位于F島的核電站,受9級特大地震影響犁跪,放射性物質(zhì)發(fā)生泄漏椿息。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一坷衍、第九天 我趴在偏房一處隱蔽的房頂上張望寝优。 院中可真熱鬧,春花似錦枫耳、人聲如沸乏矾。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽钻心。三九已至,卻和暖如春仑最,著一層夾襖步出監(jiān)牢的瞬間扔役,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工警医, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留亿胸,地道東北人。 一個月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓预皇,卻偏偏與公主長得像侈玄,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子吟温,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,884評論 2 354

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