因为一些历史原因,项目中的 C 端底部导航相关代码已经不再适用现在的需求,一些不变的逻辑和业务无需耦合在一起,代码做很多 if else 判断。刚好此项优化也是团队下半年 OKR 之一。
所以我花了一些时间思考和编写了一些代码,来做一些我理解的 “低耦合,高内聚” 的底部导航。我把他命名为 SmartNavigationLayout 。
首先说一下我们不要什么:
1 底部导航的十来个控件,我不想要他再在 MainActivity 去声明,然后在各种判断里面去做他们的选中和非选中状态处理。这部分应该内聚到 SmartNavigationLayout 中
2 点击事件需要聚合
3 不要高耦合,现在置换一个底部导航成本特别高。UI 状态处理 和 业务处理(例如: A B测、城市切换 、Web replace等) 耦合在一起。我希望 MainActivity 只需要去关心业务
再说下我们要什么
1 低耦合 MainActivity 只需要 SmartNavigationLayout 一个实例所有的方法都是 SmartNavigationLayout 的实例来提供 API 接口调用,如果 SmartNavigationLayout 不再适应需求置换成本低
2 tab 点击时间聚合
3 支持刷新某个 Tab 的样式
4 支持刷新一组 Tab 的样式
5 Tab 刷新图片支持 drawable 类型 和 http:// 开头的网络资源类型
6 支持小红点功能控制
SmartNavigationLayout
一款轻量级无依赖支持网络下发的底部导航库
核心类
TabModel
SmartNavigationLayout
TabItemView
TabModel : tab 样式的实体类,对 tab 的图片 文字 文字颜色的选中和非选中状态进行描述,采用 Builder 模式为其提供实例产生方式:
new TabModel(2)
.setImageNormal()
.setImageSelected()
.setTextColorNormal()
.setTextColorSelected()
.setText()
构造函数传参指定 tab 的 index 为必选项,其他为可选项
SmartNavigationLayout : 包含 TabItemView 的组合 View ,也是事件的发布者。对外提供 API 功能接口
使用
Step 1:
Xml
<android.support.v4.view.ViewPager
android:id="@+id/view_pager" ... />
<View
android:id="@+id/view_line" ... />
<com.julive.library.navigation.SmartNavigationLayout
android:id="@+id/smart_navigation_layout"
android:layout_width="match_parent"
android:layout_height="54dp"
android:background="#FFF"
app:layout_constraintBottom_toBottomOf="parent" />
layout/smart_navigation_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.julive.library.navigation.TabItemView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:tab_image_src="@drawable/selector_tab_home"
app:tab_text_string="@string/tab_home" />
<com.julive.library.navigation.TabItemView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:tab_image_src="@drawable/selector_tab_consultant"
app:tab_text_string="@string/tab_house" />
<com.julive.library.navigation.TabItemView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:tab_image_src="@drawable/selector_tab_qa"
app:tab_text_string="@string/tab_qa" />
<com.julive.library.navigation.TabItemView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:tab_image_src="@drawable/selector_tab_personal"
app:tab_text_string="@string/tab_personal" />
</LinearLayout>
Java
navigationLayout = findViewById(R.id.smart_navigation_layout);
navigationLayout.setOnTabItemClickListener(new OnTabItemClickListener() {
@Override
public void itemClick(int index) {
}
});
权限
<uses-permission android:name="android.permission.INTERNET" />