底部導(dǎo)航欄的實(shí)現(xiàn)方案

TabLayout+Fragment實(shí)現(xiàn)底部導(dǎo)航欄

1. 在布局中添加TabLayout

要使用TabLayout要先添加依賴


添加依賴

在布局文件中加入需要的控件

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <include layout="@layout/top_title_layout"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:alpha="0.6"
        android:background="@android:color/darker_gray"
        />

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

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:alpha="0.6"
        android:background="@android:color/darker_gray"
        />

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/bottom_tab_layout"
        app:tabIndicatorHeight="0dp"
        >
    </android.support.design.widget.TabLayout>

為了更好地設(shè)置每一個(gè)TabItem的布局磺芭,新建一個(gè)Item的布局文件

<?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:gravity="center">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tab_content_image"
        android:scaleType="centerCrop"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tab_content_title"
        android:textSize="10sp"
        />
</LinearLayout>

2. 實(shí)例化TabLayout

在Activity文件中添加實(shí)例化控件的代碼崖瞭,對(duì)TabLayout進(jìn)行監(jiān)聽,需要實(shí)現(xiàn)三個(gè)方法

       tabLayout = findViewById(R.id.bottom_tab_layout);
        // 提供自定義的布局添加Tab
        for(int i=0;i<item;i++){
            tabLayout.addTab(tabLayout.newTab().setCustomView(ItemAdapter.getTabView(this,i)));
        }
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

這里提供了一個(gè)適配Item內(nèi)容的類

public class ItemAdapter {
    public static final int[] mTabIcon = new int[]{R.drawable.ic_home_grey_500_24dp, R.drawable.ic_crop_square_grey_500_24dp, R.drawable.ic_polymer_grey_500_24dp, R.drawable.ic_blur_on_grey_500_24dp};
    public static final int[] mTabIconPressed = new int[]{R.drawable.ic_home_black_24dp, R.drawable.ic_crop_square_black_24dp, R.drawable.ic_polymer_black_24dp, R.drawable.ic_blur_on_black_24dp};
    public static final String[] mTabTitle = new String[]{"首頁(yè)", "發(fā)現(xiàn)", "關(guān)注", "我的"};

    public static Fragment[] getFragments(String from){
        Fragment fragments[] = new Fragment[4];
        fragments[0] = HomeFragment.newInstance(from);
        fragments[1] = DiscoverFragment.newInstance(from);
        fragments[2] = FollowFragment.newInstance(from);
        fragments[3] = ProfileFragment.newInstance(from);
        return fragments;
    }


    public static View getTabView(Context context, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.tab_item_layout, null);
        ImageView tabIcon = (ImageView) view.findViewById(R.id.tab_content_image);
        tabIcon.setImageResource(ItemAdapter.mTabIcon[position]);
        TextView tabText = (TextView) view.findViewById(R.id.tab_content_title);
        tabText.setText(mTabTitle[position]);
        return view;
    }
}

3. 設(shè)置點(diǎn)擊Item的變化

點(diǎn)擊每一個(gè)不同的tab就會(huì)跳轉(zhuǎn)到不同的頁(yè)面,這里新建了四個(gè)Fragment用于頁(yè)面切換吴旋,下面給出完整代碼

public class MainActivity extends AppCompatActivity {


    private TabLayout tabLayout;
    private Fragment[]mFragmensts;
    private static int item = 4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mFragmensts = ItemAdapter.getFragments("Tab");
        initTablayout();
    }

    public void initTablayout(){

        tabLayout = findViewById(R.id.bottom_tab_layout);
        // 提供自定義的布局添加Tab
        for(int i=0;i<item;i++){
            tabLayout.addTab(tabLayout.newTab().setCustomView(ItemAdapter.getTabView(this,i)));
        }
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                onTabItemSelected(tab.getPosition());
                for (int i = 0;i<tabLayout.getTabCount();i++){
                    View view = tabLayout.getTabAt(i).getCustomView();
                    ImageView icon = (ImageView)view.findViewById(R.id.tab_content_image);
                    TextView title = (TextView)view.findViewById(R.id.tab_content_title);
                    if(i == tab.getPosition()){
                        // 選中狀態(tài)
                        icon.setImageResource(ItemAdapter.mTabIconPressed[i]);
                        title.setTextColor(Color.BLACK);
                    }else{
                        // 未選中狀態(tài)
                        icon.setImageResource(ItemAdapter.mTabIcon[i]);
                        title.setTextColor(Color.DKGRAY);
                    }

                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }
    private void onTabItemSelected(int position){
        Fragment fragment = new Fragment();
        switch (position){
            case 0:
                fragment = mFragmensts[0];
                break;
            case 1:
                fragment = mFragmensts[1];
                break;

            case 2:
                fragment = mFragmensts[2];
                break;
            case 3:
                fragment = mFragmensts[3];
                break;
        }
        if(fragment!=null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.view_pager,fragment,null).commit();
        }
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末涝婉,一起剝皮案震驚了整個(gè)濱河市劲件,隨后出現(xiàn)的幾起案子未巫,更是在濱河造成了極大的恐慌,老刑警劉巖蔗草,帶你破解...
    沈念sama閱讀 221,273評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件咒彤,死亡現(xiàn)場(chǎng)離奇詭異疆柔,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)镶柱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門婆硬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人奸例,你說(shuō)我怎么就攤上這事∠蚵ィ” “怎么了查吊?”我有些...
    開封第一講書人閱讀 167,709評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)湖蜕。 經(jīng)常有香客問(wèn)我逻卖,道長(zhǎng),這世上最難降的妖魔是什么昭抒? 我笑而不...
    開封第一講書人閱讀 59,520評(píng)論 1 296
  • 正文 為了忘掉前任评也,我火速辦了婚禮,結(jié)果婚禮上灭返,老公的妹妹穿的比我還像新娘盗迟。我一直安慰自己,他們只是感情好熙含,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,515評(píng)論 6 397
  • 文/花漫 我一把揭開白布罚缕。 她就那樣靜靜地躺著,像睡著了一般怎静。 火紅的嫁衣襯著肌膚如雪邮弹。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,158評(píng)論 1 308
  • 那天蚓聘,我揣著相機(jī)與錄音腌乡,去河邊找鬼。 笑死夜牡,一個(gè)胖子當(dāng)著我的面吹牛与纽,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播氯材,決...
    沈念sama閱讀 40,755評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼渣锦,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了氢哮?” 一聲冷哼從身側(cè)響起袋毙,我...
    開封第一講書人閱讀 39,660評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎冗尤,沒(méi)想到半個(gè)月后听盖,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體胀溺,經(jīng)...
    沈念sama閱讀 46,203評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,287評(píng)論 3 340
  • 正文 我和宋清朗相戀三年皆看,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了仓坞。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,427評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡腰吟,死狀恐怖无埃,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情毛雇,我是刑警寧澤嫉称,帶...
    沈念sama閱讀 36,122評(píng)論 5 349
  • 正文 年R本政府宣布,位于F島的核電站灵疮,受9級(jí)特大地震影響织阅,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜震捣,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,801評(píng)論 3 333
  • 文/蒙蒙 一荔棉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蒿赢,春花似錦润樱、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至晾腔,卻和暖如春舌稀,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背灼擂。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工壁查, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人剔应。 一個(gè)月前我還...
    沈念sama閱讀 48,808評(píng)論 3 376
  • 正文 我出身青樓睡腿,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親峻贮。 傳聞我的和親對(duì)象是個(gè)殘疾皇子席怪,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,440評(píng)論 2 359

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