1. 程式人生 > >Android中的底部導航欄切換TabContainerView

Android中的底部導航欄切換TabContainerView

前言:在GitHub上看到一個框架,實現底部導航欄切換,感覺不錯,就在這裡總結一下。

第一步:準備工作。

在project的build.gradle中新增:

allprojects {
   	repositories {
   		...
   		maven { url 'https://jitpack.io' }
   	}
   }

在module的build.gradle中新增依賴:

implementation 'com.github.chenpengfei88:TabContainerView:v2.1'

第二步:實現步驟。

1. 佈局:

    <!--
        divideLineColor表示上面分割線的顏色
        divideLineHeight表示上面分割線的寬度
    -->
    <com.fe.library.TabContainerView
        app:divideLineColor="#afd"
        app:divideLineHeight="2"
        android:id="@+id/tab_containerview_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </com.fe.library.TabContainerView>

2. Activity:

TabContainerView tabContainerView = (TabContainerView) findViewById(R.id.tab_containerview_main);
        Fragment[] fragments = new Fragment[] {new MainFragment(), new WorkFragment(), new AppFragment(), new MineFragment(), new MineFragment()};
        int[] iconImageArray = new int[]{R.mipmap.icon_tab_home, R.mipmap.icon_tab_store, R.mipmap.icon_tab_flypig, R.mipmap.icon_tab_record, R.mipmap.icon_tab_personal};
        int[] selectedIconImageArray = new int[]{R.mipmap.icon_tab_home_pre, R.mipmap.icon_tab_store_pre, R.mipmap.icon_tab_flypig_pre, R.mipmap.icon_tab_record_pre, R.mipmap.icon_tab_personal_pre};
        String[] nameArray=new String[]{"哈哈","呵呵","嘻嘻","啦啦","咳咳"};
        /**
         * 上下文
         *  Fragment陣列
         *  Fragment管理器
         *  文字陣列
         *  文字選中時的顏色
         *  圖片預設陣列
         *  圖片選中時的陣列
         */
        tabContainerView.setAdapter(new DefaultAdapter(this, fragments, getSupportFragmentManager(),nameArray,
                getResources().getColor(R.color.colorPrimary), iconImageArray, selectedIconImageArray));
        //設定當前選中的item
        tabContainerView.setCurrentItem(1);
        //設定當前有訊息提示的item,提示小圓點
        tabContainerView.setCurrentMessageItem(1);
        //設定當前有訊息提示的item,提示小圓點,小圓點有訊息數量
        tabContainerView.setCurrentMessageItem(1, 3);
        //設定tabHost背景顏色
        tabContainerView.setTabHostBgColor(R.color.colorPrimaryDark);
        //tab切換監聽
        tabContainerView.setOnTabSelectedListener(new OnTabSelectedListener() {
            @Override
            public void onTabSelected(AbsTab absTab) {

            }
        });