實現(xiàn)底部導(dǎo)航欄及切換tab重新加載的問題解決-創(chuàng)新互聯(lián)

許多的App都使用底部導(dǎo)航欄來實現(xiàn)導(dǎo)航功能,我們可以使用RadioGroup+RadioButton的形式或者直接Button數(shù)組的方式實現(xiàn),而谷歌官方提供了FragmentTabHost來方便快捷實現(xiàn)底部導(dǎo)航欄。

站在用戶的角度思考問題,與客戶深入溝通,找到伊春網(wǎng)站設(shè)計與伊春網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站設(shè)計、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、國際域名空間、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋伊春地區(qū)。

android.support.v4.app.FragmentTabHost

實現(xiàn)底部導(dǎo)航欄及切換tab重新加載的問題解決

主要代碼:

fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.layout_content);

for (int i = 0; i < TAB_NUM; i++){
    TabHost.TabSpec tabSpec = fragmentTabHost.newTabSpec(mTitles[i]);
    tabSpec.setIndicator(getTabView(i));
    fragmentTabHost.addTab(tabSpec, mFragments[i], null);
}

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_fragment_tab_host"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.cheng.collection.ui.FragmentTabHostActivity">

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

    </FrameLayout>

    <android.support.v4.app.FragmentTabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="48dp">

    </android.support.v4.app.FragmentTabHost>
</LinearLayout>

實現(xiàn)代碼:

public class FragmentTabHostActivity extends AppCompatActivity {

    private static final int TAB_NUM = 4;

    private Class[] mFragments = new Class[]{//tab對應(yīng)的Fragment
            AFragment.class,
            BFragment.class,
            CFragment.class,
            DFragment.class
    };

    private int[] mTabDrawables = new int[]{//tab圖片
            R.drawable.tab_work,
            R.drawable.tab_im,
            R.drawable.tab_ebook,
            R.drawable.tab_me,
    };

    private String[] mTitles = new String[]{//tab標題文字
            "工作",
            "微信",
            "通信錄",
            "我的"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_tab_host);

        FragmentTabHost fragmentTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
        fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.layout_content);

        for (int i = 0; i < TAB_NUM; i++){
            TabHost.TabSpec tabSpec = fragmentTabHost.newTabSpec(mTitles[i]);
            tabSpec.setIndicator(getTabView(i));
            fragmentTabHost.addTab(tabSpec, mFragments[i], null);
        }
        //部分機型可能會顯示tab之間的分割線,設(shè)置為null取消掉
        fragmentTabHost.getTabWidget().setDividerDrawable(null);
    }

    private View getTabView(int index) {
        View view = View.inflate(this, R.layout.tab_indicator, null);
        ImageView iv = (ImageView) view.findViewById(R.id.maintab_iv);
        TextView tv = (TextView) view.findViewById(R.id.maintab_tv);

        iv.setImageDrawable(getDrawable(mTabDrawables[index]));
        tv.setText(mTitles[index]);

        return view;
    }
}

備注:

 這個實現(xiàn)方式有一個弊端,就是每次切換tab都會重新加載Fragment。實際項目過程種可能并不需要,而是需要在切換過程中保留Fragment狀態(tài)。

原因:

 FragmentTabHost在切換tab的時候使用detach和attach的方法來顯示/隱藏Fragment。

解決方法:

 修改FragmentTabHost的源代碼將doTabChanged方法中的

 ft.detach(mLastTab.fragment); 改成  ft.hide(mLastTab.fragment);

 ft.attach(newTab.fragment); 改成 ft.show(newTab.fragment);

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

分享文章:實現(xiàn)底部導(dǎo)航欄及切換tab重新加載的問題解決-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://muchs.cn/article26/dphgcg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標簽優(yōu)化、軟件開發(fā)、ChatGPT、網(wǎng)站收錄網(wǎng)站導(dǎo)航、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化