Fragment 實現(xiàn)微信Tab主界面

我們實現(xiàn)的是一個微信tab主界面昔头,效果如下:


頁面布局

由上面效果圖展示可以得知,該布局最終可由三部分組成考杉,頂部策精、中間可變內(nèi)容區(qū)域、底部

1.頂部布局崇棠,頂部布局非常簡單咽袜,就是一個LinearLayout,為其設(shè)置一個背景圖枕稀,然后里面包含一個TextView询刹,代碼如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:background="@drawable/title_bar"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="微信"
        android:textColor="#ffffff"
        android:textSize="20sp"
        android:textStyle="bold"
        />

</LinearLayout>

2.第一步,首先要在主界面布局activity_main.xml中添加FrameLayout代碼如下:

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

這里只是給FrameLayout的id設(shè)置成id_container萎坷,并沒有在里面添加任何具體的內(nèi)容凹联,因為具體的內(nèi)容要在后面動態(tài)的添加的。這里設(shè)置android:layout_weight="1"意思是哆档,除了頂部和底部(因為頂部和底部高度都設(shè)定好了)的高度外蔽挠,其余的高度都被FramLayout占有。

3.底部設(shè)計瓜浸,我們可以這樣設(shè)計底部:可以將底部(水平的LinearLayout)分成四塊(四個垂直的LinearLayout)澳淑,每一塊(LinearLayout)包含一個ImageView和一個TextView.ImageView用來顯示當(dāng)前Tab圖標(biāo)比原,TextView用來顯示當(dāng)前Tab的標(biāo)題,代碼如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="@drawable/bottom_bar"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/id_tab_weixin"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/id_tab_weixin_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00000000"
            android:clickable="false"
            android:src="@drawable/tab_weixin_pressed" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="微信"
            android:textColor="#fff" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/id_tab_frd"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/id_tab_frd_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00000000"
            android:clickable="false"
            android:src="@drawable/tab_find_frd_normal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="朋友"
            android:textColor="#fff" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/id_tab_address"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/id_tab_address_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00000000"
            android:clickable="false"
            android:src="@drawable/tab_address_normal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="通訊錄"
            android:textColor="#fff" />

    </LinearLayout>


    <LinearLayout
        android:id="@+id/id_tab_setting"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/id_tab_setting_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00000000"
            android:clickable="false"
           android:src="@drawable/tab_settings_normal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="設(shè)置"
            android:textColor="#fff" />

    </LinearLayout>

</LinearLayout>

代碼實現(xiàn)控制

1.Fragment的實現(xiàn)
因為我們有四個Tab杠巡,正常來說春寿,我們要實現(xiàn)四個Fragment和他們的布局,如下(以第一個微信Tab為例):

public class WeixinFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.tab01, container, false);

    }
}   

其他三個也依次創(chuàng)建其他三個布局的Fragment忽孽,這是一般做法。因為這四個布局的Fragment的代碼基本一致谢床,所以我考慮能不能只寫一個Fragment就可以實現(xiàn)這四個Fragment的功能兄一,最終寫下以下代碼,敬請指導(dǎo):

public class WeixinFragment extends Fragment {

    private int layoutID;
    //優(yōu)化后的HashMap
    SparseArray<View> views;
    View rootView;

    /**
     * 構(gòu)造帶參構(gòu)造函數(shù)
     * @param layoutID  布局的ID
     * @param context   傳入上下文
     */
    public WeixinFragment(int layoutID, Context context) {
        this.layoutID = layoutID;
        views = new SparseArray<>();
        rootView = LayoutInflater.from(context).inflate(layoutID,null);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return rootView;

    }
}

2.在MainACtivity中首先初始化4個Fragment

fragments是HashMap<Integer, WeixinFragment> 類型识腿,初始化4個Fragment出革,就是將這個4個Tab對應(yīng)的布局加入到HashMap中。

private void initFragments() {
        fragments.put(R.layout.tab01, new WeixinFragment(R.layout.tab01, this));
        fragments.put(R.layout.tab02, new WeixinFragment(R.layout.tab02, this));
        fragments.put(R.layout.tab03, new WeixinFragment(R.layout.tab03, this));
        fragments.put(R.layout.tab04, new WeixinFragment(R.layout.tab04, this));
    }

3.初始化控件

private void initView() {
//這四個是底部的四個LinearLayout初始化
        mTabWeixin = (LinearLayout) findViewById(R.id.id_tab_weixin);
        mTabFrd = (LinearLayout) findViewById(R.id.id_tab_frd);
        mTabAdress = (LinearLayout) findViewById(R.id.id_tab_address);
        mTabSetting = (LinearLayout) findViewById(R.id.id_tab_setting);
//下面四個是底部圖標(biāo)初始化
        mWeixinImg = (ImageButton) findViewById(R.id.id_tab_weixin_img);
        mFrdImg = (ImageButton) findViewById(R.id.id_tab_frd_img);
        mAddressImg = (ImageButton) findViewById(R.id.id_tab_address_img);
        mSettingnImg = (ImageButton) findViewById(R.id.id_tab_setting_img);

    }

4.設(shè)置點擊事件

為底部四個LinearLayout設(shè)置點擊事件渡讼,這樣不管時點擊底部的圖標(biāo)還是底部的文字部分都會觸發(fā)圖標(biāo)和內(nèi)容區(qū)域的變化骂束。代碼如下:

private void initEvent() {
        mTabWeixin.setOnClickListener(this);
        mTabFrd.setOnClickListener(this);
        mTabAdress.setOnClickListener(this);
        mTabSetting.setOnClickListener(this);

    }

5.onClick方法實現(xiàn)

該方法是設(shè)置點擊事件自動生成的,在這里需要我們判斷是哪個觸發(fā)了事件成箫,然后執(zhí)行相應(yīng)的操作展箱。比如,如果是點擊了底部微信的圖標(biāo)(或文字)時蹬昌,則應(yīng)將微信的圖標(biāo)變亮混驰,并將中間內(nèi)容區(qū)域變成相應(yīng)區(qū)域,如剛開始的效果展示圖皂贩。代碼如下:

 @Override
    public void onClick(View view) {

//在觸發(fā)相應(yīng)事件之前栖榨,應(yīng)先將所有圖標(biāo)變暗,該函數(shù)就是實現(xiàn)該功能
        resetImg();

        switch (view.getId()) {
            case R.id.id_tab_weixin:
           //如果點擊的是微信圖標(biāo)明刷,就跳轉(zhuǎn)到第一個tab
                setSelect(0);
                break;

            case R.id.id_tab_frd:
                setSelect(1);
                break;

            case R.id.id_tab_address:
                setSelect(2);
                break;

            case R.id.id_tab_setting:
                setSelect(3);
                break;
            default:
                break;
        }
    }
    
     /**
     * 將所有的圖片切換為暗色
     */
    private void resetImg() {

        mWeixinImg.setImageResource(R.drawable.tab_weixin_normal);
        mFrdImg.setImageResource(R.drawable.tab_find_frd_normal);
        mAddressImg.setImageResource(R.drawable.tab_address_normal);
        mSettingnImg.setImageResource(R.drawable.tab_settings_normal);

    }    
    

6.setSelect函數(shù)實現(xiàn)

該函數(shù)是用來實現(xiàn)根據(jù)點擊底部圖標(biāo)進行中間FramLayout內(nèi)容變化的控制部分

 private void setSelect(int i) {
        //將所選圖片變?yōu)榱辽?        //切換對應(yīng)的內(nèi)容區(qū)域

        //首先拿到Fragment的內(nèi)容管理器
        FragmentManager fm = getSupportFragmentManager();
        //開啟事務(wù)
        FragmentTransaction transaction = fm.beginTransaction();
        //i代表要填充的是哪個頁面
        switch (i) {
            case 0:   
//將FramLayout的內(nèi)容填充為Tab01            transaction.replace(R.id.id_container,fragments.get(R.layout.tab01));
            //將圖標(biāo)微信變成亮色
 mWeixinImg.setImageResource(R.drawable.tab_weixin_pressed);
                break;
            case 1:
                transaction.replace(R.id.id_container,fragments.get(R.layout.tab02));
                mFrdImg.setImageResource(R.drawable.tab_find_frd_pressed);
                break;

            case 2:

                //如果mTab01沒有初始化婴栽,則進行初始化
                transaction.replace(R.id.id_container,fragments.get(R.layout.tab03));
                mAddressImg.setImageResource(R.drawable.tab_address_pressed);
                break;

            case 3:

                transaction.replace(R.id.id_container,fragments.get(R.layout.tab04));
                mSettingnImg.setImageResource(R.drawable.tab_settings_pressed);
                break;

            default:
                break;
        }
        //最后事務(wù)進行提交
        transaction.commit();
    }

源碼下載

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市辈末,隨后出現(xiàn)的幾起案子愚争,更是在濱河造成了極大的恐慌,老刑警劉巖本冲,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件准脂,死亡現(xiàn)場離奇詭異,居然都是意外死亡檬洞,警方通過查閱死者的電腦和手機狸膏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來添怔,“玉大人湾戳,你說我怎么就攤上這事贤旷。” “怎么了砾脑?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵幼驶,是天一觀的道長。 經(jīng)常有香客問我韧衣,道長盅藻,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任畅铭,我火速辦了婚禮氏淑,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘硕噩。我一直安慰自己假残,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布炉擅。 她就那樣靜靜地躺著辉懒,像睡著了一般。 火紅的嫁衣襯著肌膚如雪谍失。 梳的紋絲不亂的頭發(fā)上眶俩,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天,我揣著相機與錄音袱贮,去河邊找鬼仿便。 笑死,一個胖子當(dāng)著我的面吹牛攒巍,可吹牛的內(nèi)容都是我干的嗽仪。 我是一名探鬼主播,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼柒莉,長吁一口氣:“原來是場噩夢啊……” “哼闻坚!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起兢孝,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤窿凤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后跨蟹,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體雳殊,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年窗轩,在試婚紗的時候發(fā)現(xiàn)自己被綠了夯秃。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖仓洼,靈堂內(nèi)的尸體忽然破棺而出介陶,到底是詐尸還是另有隱情,我是刑警寧澤色建,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布哺呜,位于F島的核電站,受9級特大地震影響箕戳,放射性物質(zhì)發(fā)生泄漏某残。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一陵吸、第九天 我趴在偏房一處隱蔽的房頂上張望驾锰。 院中可真熱鬧,春花似錦走越、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至喳整,卻和暖如春谆构,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背框都。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工搬素, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人魏保。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓熬尺,卻偏偏與公主長得像,于是被迫代替她去往敵國和親谓罗。 傳聞我的和親對象是個殘疾皇子粱哼,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,611評論 2 353

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,077評論 25 707
  • 當(dāng)開始一個新項目的時候,有一個很重要的步驟就是確定我們的APP首頁框架檩咱,也就是用戶從桌面點擊APP 圖標(biāo)揭措,進入AP...
    依然范特稀西閱讀 51,568評論 16 238
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件刻蚯、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,094評論 4 62
  • 阿伯绊含,是個很老實巴交的人。 話說這天是村里分發(fā)過節(jié)禮物的時候炊汹。阿伯興沖沖的走到村長家院子里躬充,等著領(lǐng)東西。 發(fā)呆的他...
    壹芽閱讀 171評論 0 0
  • 今天早上,爸爸說麻裳,拿了你手機中的手機卡口蝠。意味著你完全不能上網(wǎng)了。 爸爸問我支不支持他津坑? 我說當(dāng)然支持妙蔗,當(dāng)你對一件事...
    躲進小樓看燈火閱讀 270評論 0 1