Android頂部導(dǎo)航欄TabLayout+ViewPage

上一篇文章已經(jīng)詳細(xì)介紹了底部導(dǎo)航欄的實(shí)現(xiàn)蛋济,這篇是在上一篇的基礎(chǔ)上加上頂部導(dǎo)航实束,不懂的可以看我的上一篇文章诈闺。地址如下:

Android底部導(dǎo)航實(shí)現(xiàn)的簡便方法RadioGroup+Fragment

因?yàn)楹芏郺pp都是底部導(dǎo)航和頂部導(dǎo)航嵌套使用煤辨,所以現(xiàn)在我講的也是在上一篇博客的代碼基礎(chǔ)下竞滓,在底部導(dǎo)航的Fragment中使用頂部導(dǎo)航,相信如果你學(xué)會了怎么在Fragment使用頂部導(dǎo)航疫向,在Activity中使用的話對你來說應(yīng)該是小兒科咳蔚,回歸正題。

頂部導(dǎo)航欄TabLayout+ViewPager

1.修改上一篇的首頁布局文件即fragment_home.xml,里面主要由TabLayout和ViewPager組成搔驼。Tablayout布局頂部導(dǎo)航谈火,ViewPager布局各導(dǎo)航的內(nèi)容。同時新建一個frament_tab的布局舌涨,里面就一個TextView糯耍,便于分辨出各導(dǎo)航,可根據(jù)所需功能自行修改。

fragment_home.xml

<?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"
    android:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="40dp">

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

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

    </android.support.v4.view.ViewPager>

</LinearLayout>

frament_tab

<?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_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="關(guān)注"
        android:textSize="20sp"
        android:gravity="center"/>

</LinearLayout>

2.新建一個fragment類,TabFragment,注意繼承時繼承android.support.v4.app.Fragment温技,這個fragment的作用就是各導(dǎo)航的內(nèi)容

public class TabFragment extends Fragment {
    private TextView titleTv;

    private String mTitle;

    //這個構(gòu)造方法是便于各導(dǎo)航同時調(diào)用一個fragment
    public TabFragment(String title){
        mTitle=title;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
        View view=inflater.inflate(R.layout.fragment_tab,container,false);
        titleTv=view.findViewById(R.id.tv_title);
        titleTv.setText(mTitle);
        return view;
    }
}

3.新建ViewPager的設(shè)配器革为,繼承FragmentPagerAdapter

public class FragmentAdapter extends FragmentPagerAdapter {


    private List<TabFragment> mFragmentList;//各導(dǎo)航的Fragment
    private List<String> mTitle; //導(dǎo)航的標(biāo)題

    public FragmentAdapter(FragmentManager fragmentManager,List<TabFragment>fragments,List<String>title){
        super(fragmentManager);
        mFragmentList=fragments;
        mTitle=title;

    }
    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }
    @Override
    public CharSequence getPageTitle(int position) {
        return mTitle.get(position);
    }
}

4.最后只需要在HomeFragment布局渲染即可。

public class HomeFragment extends Fragment{
    private ViewPager pager;
    private FragmentAdapter fragmentAdapter;
    private List<TabFragment> fragmentList;
    private TabLayout tabLayout;
    private TabFragment fragment1,fragment2,fragment3,fragment4,fragment5;
    private List<String> mTitles;
    private String [] title={"關(guān)注","推薦","廣州","視頻","熱點(diǎn)"};
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
        View view=inflater.inflate(R.layout.fragment_home,container,false);
        pager=view.findViewById(R.id.page);
        tabLayout=view.findViewById(R.id.tab_layout);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        fragmentList=new ArrayList<>();
        mTitles=new ArrayList<>();
        for(int i=0;i<title.length;i++){
            mTitles.add(title[i]);
            fragmentList.add(new TabFragment(title[i]));
        }

        fragmentAdapter=new FragmentAdapter(getActivity().getSupportFragmentManager(),fragmentList,mTitles);
        pager.setAdapter(fragmentAdapter);
        tabLayout.setupWithViewPager(pager);//與ViewPage建立關(guān)系
    }

}

效果圖:

這里寫圖片描述

如有問題歡迎在評論區(qū)指出舵鳞!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末震檩,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子蜓堕,更是在濱河造成了極大的恐慌抛虏,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件套才,死亡現(xiàn)場離奇詭異嘉蕾,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)霜旧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來儡率,“玉大人挂据,你說我怎么就攤上這事《眨” “怎么了崎逃?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長眉孩。 經(jīng)常有香客問我个绍,道長,這世上最難降的妖魔是什么浪汪? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任巴柿,我火速辦了婚禮,結(jié)果婚禮上死遭,老公的妹妹穿的比我還像新娘广恢。我一直安慰自己,他們只是感情好呀潭,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布钉迷。 她就那樣靜靜地躺著,像睡著了一般钠署。 火紅的嫁衣襯著肌膚如雪糠聪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天谐鼎,我揣著相機(jī)與錄音舰蟆,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛夭苗,可吹牛的內(nèi)容都是我干的信卡。 我是一名探鬼主播,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼题造,長吁一口氣:“原來是場噩夢啊……” “哼傍菇!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起界赔,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤丢习,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后淮悼,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體咐低,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年袜腥,在試婚紗的時候發(fā)現(xiàn)自己被綠了见擦。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡羹令,死狀恐怖鲤屡,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情福侈,我是刑警寧澤酒来,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站肪凛,受9級特大地震影響堰汉,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜伟墙,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一翘鸭、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧戳葵,春花似錦矮固、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至邻梆,卻和暖如春守伸,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背浦妄。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工尼摹, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留见芹,地道東北人。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓蠢涝,卻偏偏與公主長得像玄呛,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子和二,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,047評論 2 355

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