Net属于低耦合框架, 自动下拉刷新需要依赖第三方组件: BRV(点击链接按文档依赖)
使用固定版本号替换+符号
implementation 'com.github.liangjingkanji:BRV:+'
当然如果自己处理下拉刷新也是可以的, Net可以仅仅作为网络框架存在
创建PageRefreshLayout
<com.drake.brv.PageRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/page"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.fragment.PushRefreshFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_push"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.drake.brv.PageRefreshLayout>
创建列表
rv_push.linear().setup {
addType<String>(R.layout.item_list)
}
创建网络请求
page.onRefresh {
scope {
// 请求到数据设置到RecyclerView
rv_push.models = Get<ListModel>("list").await().data.list
}
}.autoRefresh()
注意高亮处使用的是
scope
而不是其他作用域, 只能使用scope, 否则无法跟随PageRefreshLayout生命周期等功能
|生命周期|描述|
|-|-|
|开始|PageRefreshLayout执行showLoading/autoRefresh
后触发onRefresh
, 然后开始网络请求|
|结束|PageRefreshLayout被销毁(例如其所在的Activity或Fragment被销毁), 网络请求自动取消|