22
33## 准备工作
44
5- 需要修改基类,只需简单的几步就可以把本库的功能集成到基类,并且不会影响到已有的代码,只是给基类增加了新的方法。
5+ 需要修改基类,只需简单的几步就可以把本库的功能集成到基类,并且不会影响到已有的代码,只是给基类扩展了新的方法。
6+
7+ 添加依赖:
8+
9+ ``` groovy
10+ dependencies {
11+ implementation 'com.github.DylanCaiCoding.LoadingStateView:loadingstateview-ktx:4.0.0'
12+ }
13+ ```
614
715修改步骤如下:
816
9- 1 . 增加 ` LoadingState by LoadingStateImpl(), OnReloadListener ` 代码,这是给基类实现 ` LoadingState ` 和 ` OnReloadListener ` 接口,并且把 ` LoadingState ` 接口委托给 ` LoadingStateImpl ` 。
17+ 1 . 增加 ` LoadingState by LoadingStateImpl(), OnReloadListener ` 代码,这是给基类实现 ` LoadingState ` 和 ` OnReloadListener ` 接口,并且把 ` LoadingState `
18+ 接口委托给 ` LoadingStateImpl ` 。
10192 . 在基类中添加 ` open val isDecorated = true ` 属性,提供一个可撤销的配置变量。
11- 3 . 在 Activity 的 ` setContentView() ` 方法后执行 ` decorateContentView(this, isDecorated) ` 。在 Fragment 的 ` onCreateView() ` 返回值执行 ` view.decorate(this, isDecorated) ` 。
20+ 3 . 在 Activity 的 ` setContentView() ` 方法后执行 ` decorateContentView(this, isDecorated) ` 。在 Fragment 的 ` onCreateView() `
21+ 返回值执行 ` view.decorate(this, isDecorated) ` 。
1222
1323<!-- tabs:start -->
1424
2434
2535这样改造基类后会得到以下的增强:
2636
27- - 在不影响已有代码的情况下,增加了 [ LoadingState] ( https://github.com/DylanCaiCoding/LoadingStateView/blob/master/loadingstateview-ktx/src/main/java/com/dylanc/loadingstateview/LoadingState.kt ) 和 ` OnReloadListener ` 提供的常用方法。
37+ - 在不影响已有代码的情况下,增加了 [ LoadingState] ( https://github.com/DylanCaiCoding/LoadingStateView/blob/master/loadingstateview-ktx/src/main/java/com/dylanc/loadingstateview/LoadingState.kt )
38+ 和 ` OnReloadListener ` 提供的常用方法。` LoadingState ` 接口包含了 ` LoadingStateView ` 所有功能。
39+
2840- 如果担心万一会遇到什么兼容问题,那么在页面重写 ` override val isDecorated = false ` 属性就会把新增的功能禁用,即使调用了接口方法也不会生效。
2941
30- ### 注册默认的标题栏和缺省页样式
42+ ## 显示缺省页
3143
3244缺省页需要创建一个类继承 ` LoadingStateView.ViewDelegate ` ,构造函数需要传个视图类型参数,默认提供了 ` ViewType.LOADING ` 、` ViewType.ERROR ` 、` ViewType.EMPTY ` 。
3345
3749
3850``` kotlin
3951class LoadingViewDelegate : LoadingStateView .ViewDelegate (ViewType .LOADING ) {
40-
52+
4153 override fun onCreateView (inflater : LayoutInflater , parent : ViewGroup ): View =
4254 inflater.inflate(R .layout.layout_loading, parent, false )
4355}
@@ -66,15 +78,54 @@ class EmptyViewDelegate : LoadingStateView.ViewDelegate(ViewType.EMPTY) {
6678 inflater.inflate(R .layout.layout_empty, parent, false )
6779}
6880```
69-
81+
7082<!-- tabs:end -->
7183
72- 标题栏的稍微有点不同,需要创建一个类继承 ` BaseToolbarViewDelegate ` ,比 ` LoadingStateView.ViewDelegate ` 多重写个 ` bind(config) ` 方法,方便之后的更新操作。
84+ 在 Application 注册全局的 ` ViewDelegate ` 。
85+
86+ ``` kotlin
87+ LoadingStateView .setViewDelegatePool {
88+ register(LoadingViewDelegate (), ErrorViewDelegate (), EmptyViewDelegate ())
89+ }
90+ ```
91+
92+ 在实现了基类的 ` Activity ` 或 ` Fragment ` 可以调用对应的 showView() 方法。
93+
94+ ``` kotlin
95+ showView(viewType)
96+ showLoadingView() // 显示 ViewType.LOADING 类型的视图
97+ showContentView() // 显示 ViewType.CONTENT 类型的视图
98+ showErrorView() // 显示 ViewType.ERROR 类型的视图
99+ showEmptyView() // 显示 ViewType.EMPTY 类型的视图
100+ ```
101+
102+ 如果某个页面需要显示不同的缺省页,可以在显示前调用一下 ` registerView(viewDelegate) ` 方法覆盖全局的样式。比如:
103+
104+ ``` kotlin
105+ registerView(CoolLoadingViewDelegate ())
106+ showLoadingView()
107+ ```
108+
109+ 如果需要动态更新某个样式,在 ` ViewDelegate ` 自行增加更新的方法,比如在 ` ErrorViewDelegate ` 增加了 ` updateMsg(msg) ` 方法修改请求失败的文字,然后就能更新了。
110+
111+ <!-- tabs:start -->
112+
113+ #### ** Kotlin**
114+
115+ ``` kotlin
116+ loadingStateView.updateViewDelegate<ErrorViewDelegate >(ViewType .ERROR ) {
117+ updateMsg(" 服务器繁忙,请稍后重试" )
118+ }
119+ ```
120+
121+ ## 添加标题栏
73122
74- 下面用个简单标题栏为例子,只有一个返回键和右侧按钮。
123+ 标题栏的稍微有点不同,需要创建一个类继承 ` BaseToolbarViewDelegate ` ,比 ` LoadingStateView.ViewDelegate ` 多重写个 ` onBindToolbar(config) ` 方法,方便之后的更新操作。
124+
125+ 通常项目都有各自的标题栏封装,我们能基于已有的标题栏布局或者自定义的标题栏控件实现 ` ToolbarViewDelegate ` 。比如:
75126
76127``` kotlin
77- class DefaultToolbarViewDelegate : ToolbarViewDelegate () {
128+ class ToolbarViewDelegate : BaseToolbarViewDelegate () {
78129 private lateinit var tvTitle: TextView
79130 private lateinit var ivLeft: ImageView
80131 private lateinit var ivRight: ImageView
@@ -87,7 +138,7 @@ class DefaultToolbarViewDelegate : ToolbarViewDelegate() {
87138 return view
88139 }
89140
90- override fun bind (config : ToolbarConfig ) {
141+ override fun onBindToolbar (config : ToolbarConfig ) {
91142 tvTitle.text = config.title
92143
93144 if (config.navBtnType == NavBtnType .NONE ) {
@@ -106,12 +157,80 @@ class DefaultToolbarViewDelegate : ToolbarViewDelegate() {
106157}
107158```
108159
109- 通常项目都有各自的标题栏封装,比如 ` <include/> ` 的标题栏布局或者自定义的标题栏控件,我们能基于此实现 。
160+ ` ToolbarConfig ` 提供了几个常用的属性。可以根据需要选择处理,比如上述例子只实现了有无返回键和右侧按钮的逻辑,项目中有功能相对完整的 [ 示例代码 ] ( https://github.com/DylanCaiCoding/LoadingStateView/blob/master/sample-kotlin/src/main/java/com/dylanc/loadingstateview/sample/kotlin/delegate/ToolbarViewDelegate.kt ) 。
110161
111- 在 Application 注册全局的 ` ViewDelegate ` 。
162+ | 属性 | 含义 |
163+ | -------------------- | -------------------- |
164+ | title | 标题 |
165+ | navBtnType | 导航 (左侧) 按钮类型 |
166+ | navIcon | 导航 (左侧) 图标 |
167+ | navText | 导航 (左侧) 文字 |
168+ | onNavClickListener | 导航 (左侧) 按钮点击事件 |
169+ | rightIcon | 右侧图标 |
170+ | rightText | 右侧文字 |
171+ | onRightClickListener | 右侧按钮点击事件 |
172+
173+ ` onNavClickListener ` 默认执行 ` finish() ` 操作。` navBtnType ` 默认类型是 ` NavBtnType.ICON ` ,还有 ` NavBtnType.NONE ` 、` NavBtnType.TEXT ` 、` NavBtnType.ICON_TEXT ` 类型。
174+
175+ 当然这点属性肯定不能满足所有的需求,所以本库支持给 ` ToolbarConfig ` 增加扩展属性。比如需要动态修改右侧文字颜色:
176+
177+ ``` kotlin
178+ var ToolbarConfig .rightTextColor: Int? by toolbarExtras() // 增加 rightTextColor 扩展属性
179+
180+ class ToolbarViewDelegate : BaseToolbarViewDelegate () {
181+
182+ // ...
183+
184+ override fun onBindToolbar (config : ToolbarConfig ) {
185+ // ...
186+ config.rightTextColor?.let { tvRight.setTextColor(it) } // 处理扩展属性
187+ }
188+ }
189+ ```
190+
191+ 在 Application 注册全局的标题栏 ` ViewDelegate ` 。
112192
113193``` kotlin
114194LoadingStateView .setViewDelegatePool {
115- register(DefaultToolbarViewDelegate (), LoadingViewDelegate (), ErrorViewDelegate (), EmptyViewDelegate ())
195+ register(ToolbarViewDelegate (), // ... )
196+ }
197+ ```
198+
199+ 之后就能在实现了基类的 `Activity ` 或 `Fragment ` 设置标题栏了。
200+
201+ ```kotlin
202+ setToolbar() // 默认有返回键
203+
204+ setToolbar(" title" ) // 有标题和返回键
205+
206+ setToolbar(" title" , NavBtnType .NONE ) // 只有标题,无返回键
207+
208+ setToolbar(" title" ) { // 以下可选
209+ navIcon = R .drawable.account // 只修改返回键图标
210+ navIcon { .. . } // 只修改返回键的点击事件
211+ navIcon(R .drawable.message) { .. . } // 修改返回键的图标和点击事件
212+ rightIcon(R .drawable.add) { .. . } // 添加右侧图标
213+ rightText(" Delete" ) { .. . } // 添加右侧文字
214+ rightTextColor = Color .RED // 新增的扩展属性,修改右侧文字颜色
116215}
117216```
217+
218+ 如果样式变动很大,不建议写太多扩展属性来配置,这样代码阅读性差。推荐用新布局再写一个 `BaseToolbarViewDelegate ` 的实现类,在设置标题栏之前注册覆盖掉默认的样式。
219+
220+ ```kotlin
221+ registerView(SpecialToolbarViewDelegate ())
222+ setToolbar(" title" )
223+ ```
224+
225+ ## 在顶部添加多个控件
226+
227+ 比如添加标题栏和搜索栏,搜索栏需要另写一个类继承 `ViewDelegate `。
228+
229+ ```kotlin
230+ setHeaders(
231+ ToolbarViewDelegate (" Search" ) {
232+ rightIcon(R .drawable.more) { .. . }
233+ },
234+ SearchViewDelegate (onSearchListener)
235+ )
236+ ```
0 commit comments