!!! success "模块化依赖"
如果自己处理缺省页可跳过本章, Net可以仅仅作为简单的网络框架存在
Net可依赖三方库实现自动缺省页, 以下二选一依赖
依赖 StateLayout
implementation 'com.github.liangjingkanji:StateLayout:+' // 使用固定版本号替换+符号
依赖 BRV (因为BRV包含StateLayout)
implementation 'com.github.liangjingkanji:BRV:+' // 使用固定版本号替换+符号
在Application中初始化缺省页
StateConfig.apply {
emptyLayout = R.layout.layout_empty
loadingLayout = R.layout.layout_loading
errorLayout = R.layout.layout_error
}
使用StateLayout
包裹的内容即内容
(content)
<com.drake.statelayout.StateLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/state"
android:layout_height="match_parent"
tools:context=".ui.fragment.StateLayoutFragment">
<TextView
android:id="@+id/tv"
android:padding="32dp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="内容" />
</com.drake.statelayout.StateLayout>
加载中
缺省页内容
缺省页请求失败, 显示错误
缺省页
state.onRefresh {
scope {
tv.text = Get<String>(Api.PATH).await()
}
}.showLoading()
生命周期 | 描述 |
---|---|
开始 | showLoading 触发onRefresh , 开始请求 |
结束 | 缺省页被销毁, 请求自动取消 |