前言:在GitHub上看到一个框架,实现底部导航栏切换,感觉不错,就在这里总结一下。
参考:https://www.jianshu.com/p/9aaff43bbf9f
https://github.com/chenpengfei88/TabContainerView
第一步:准备工作。
在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) {
}
});