ViewModel+LiveData Android Architecture Components
Paging Library Paging
Paging
1. Datasource
数据源抽象类,Paging有三种实现
(1)PageKeyedDataSource 按页加载,如请求数据时传入page页码。
(2)ItemKeyedDataSource 按条目加载,即请求数据需要传入其它item的信息,如加载第n+1项的数据需传入第n项的id。
(3)PositionalDataSource 按位置加载,如加载指定从第n条到n+20条。
2. PagedList
PagedList是List的子类,通过Datasource加载数据,并可以设置一次加载的数量以及预加载的数量等。
3.PagedListAdapter
PagedListAdapte是RecyclerView.Adapter的实现,用于展示PagedList的数据。数据源变动时后台线程通过DiffUtil比较前后两个PagedList的差异,然后调用notifyItem...()方法更新RecyclerView。
4. LivePagedListBuilder
将PagedList和LiveData整合成LiveData<PagedList>。
Paging+BoundaryCallback封装
网上许多demo都是基于google example,官方example主要是教我们怎么用,也有很多值得参考的地方,但直接搬项目中去用却明显水土不服,建议先理解了google example中Paging的用法,再自己封装下...
定义列表的几种状态
/**
* 列表状态
*/
sealed class ListStatus
/**
* 初始化中
*/
class Initialize : ListStatus()
/**
* 初始化成功
*/
class InitializeSuccess : ListStatus()
/**
* 初始化错误
*/
class InitializeError : ListStatus()
/**
* 列表空
*/
class Empty : ListStatus()
/**
* 列表往下加载更多中
*/
class LoadMoreIn : ListStatus()
/**
* 列表往下加载成功
*/
class LoadMoreSuccess : ListStatus()
/**
* 列表往下加载失败
*/
class LoadMoreError : ListStatus()
/**
* 列表往下已全部加载完毕
*/
class End : ListStatus()
/**
* 列表往上加载更多中
*/
class AtFrontLoadMoreIn : ListStatus()
/**
* 列表往上加载成功
*/
class AtFrontLoadMoreSuccess : ListStatus()
/**
* 列表往上加载失败
*/
class AtFrontLoadMoreError : ListStatus()
/**
* 列表往上已全部加载完毕
*/
class AtFrontEnd : ListStatus()
自定义BoundaryCallback
class ListBoundaryCallback<Value>(private val listLiveData: MutableLiveData<ListStatus>) : PagedList.BoundaryCallback<Value>() {
override fun onZeroItemsLoaded() {
super.onZeroItemsLoaded()
listLiveData.value = Empty()
}
override fun onItemAtEndLoaded(itemAtEnd: Value) {
super.onItemAtEndLoaded(itemAtEnd)
listLiveData.value = End()
}
override fun onItemAtFrontLoaded(itemAtFront: Value) {
super.onItemAtFrontLoaded(itemAtFront)
listLiveData.value = AtFrontEnd()
}
}
BoundaryCallback
是Datasource
中的数据加载到边界时的回调,为PagedList
设置BoundaryCallback
用来监听加载本地数据的事件,对应回调方法中为ListStatus
设置不同状态
公共ViewModel
open class MyAndroidViewModel(application: Application) : AndroidViewModel(application) {
private fun makePagedListConfig(pageSize: Int = 20): PagedList.Config =
PagedList.Config.Builder().apply {
setPageSize(pageSize)
setInitialLoadSizeHint(pageSize)
setPrefetchDistance(2)
setEnablePlaceholders(false)
}.build()
fun <Key, Value> makePagedList(dataSourceFactory: DataSource.Factory<Key, Value>,
listStatus: MutableLiveData<ListStatus>, pageSize: Int = 20): LiveData<PagedList<Value>> =
LivePagedListBuilder<Key, Value>(dataSourceFactory, makePagedListConfig(pageSize))
.setBoundaryCallback(ListBoundaryCallback(listStatus))
.build()
}
ViewModel中设置PagedList.Config
,初始化LivePagedListBuilder
自定义PageKeyedDataSource及相关回调
abstract class ListStatusPageKeyedDataSource<Key, Value>(private val status: MutableLiveData<ListStatus>) : PageKeyedDataSource<Key, Value>() {
final override fun loadInitial(params: LoadInitialParams<Key>, callback: LoadInitialCallback<Key, Value>) {
status.postValue(Initialize())
onLoadInitial(params, ListStatusPageKeyedLoadInitialCallback(callback, status))
}
abstract fun onLoadInitial(params: LoadInitialParams<Key>, callback: ListStatusPageKeyedLoadInitialCallback<Key, Value>)
final override fun loadAfter(params: LoadParams<Key>, callback: LoadCallback<Key, Value>) {
status.postValue(LoadMoreIn())
onLoadAfter(params, ListStatusPageKeyedLoadCallback(callback, status))
}
abstract fun onLoadAfter(params: LoadParams<Key>, callback: ListStatusPageKeyedLoadCallback<Key, Value>)
final override fun loadBefore(params: LoadParams<Key>, callback: LoadCallback<Key, Value>) {
status.postValue(AtFrontLoadMoreIn())
onLoadBefore(params, ListStatusAtFrontPageKeyedLoadCallback(callback, status))
}
abstract fun onLoadBefore(params: LoadParams<Key>, callback: ListStatusAtFrontPageKeyedLoadCallback<Key, Value>)
}
PageKeyedDataSource
是数据源, 将这几种回调替换成我们自定义的回调并抽象出来,改变对应Status状态。
class ListStatusPageKeyedLoadInitialCallback<Key, Value>(
private val callback: PageKeyedDataSource.LoadInitialCallback<Key, Value>,
private val listStatus: MutableLiveData<ListStatus>) : PageKeyedDataSource.LoadInitialCallback<Key, Value>() {
override fun onResult(data: MutableList<Value>, position: Int, totalCount: Int, previousPageKey: Key?, nextPageKey: Key?) {
callback.onResult(data, position, totalCount, previousPageKey, nextPageKey)
if (!data.isEmpty()) {
listStatus.postValue(InitializeSuccess())
} else {
// 当列表为空时 BoundaryCallback 会回调 Empty 状态因此这里不用处理
}
}
override fun onResult(data: MutableList<Value>, previousPageKey: Key?, nextPageKey: Key?) {
callback.onResult(data, previousPageKey, nextPageKey)
if (!data.isEmpty()) {
listStatus.postValue(InitializeSuccess())
} else {
// 当列表为空时 BoundaryCallback 会回调 Empty 状态因此这里不用处理
}
}
fun onError() {
listStatus.postValue(InitializeError())
}
}
class ListStatusPageKeyedLoadCallback<Key, Value>(
private val callback: PageKeyedDataSource.LoadCallback<Key, Value>,
private val listStatus: MutableLiveData<ListStatus>) : PageKeyedDataSource.LoadCallback<Key, Value>() {
override fun onResult(data: MutableList<Value>, adjacentPageKey: Key?) {
callback.onResult(data, adjacentPageKey)
listStatus.postValue(LoadMoreSuccess())
}
fun onError() {
listStatus.postValue(LoadMoreError())
}
}
class ListStatusAtFrontPageKeyedLoadCallback<Key, Value>(
private val callback: PageKeyedDataSource.LoadCallback<Key, Value>,
private val listStatus: MutableLiveData<ListStatus>) : PageKeyedDataSource.LoadCallback<Key, Value>() {
override fun onResult(data: MutableList<Value>, adjacentPageKey: Key?) {
callback.onResult(data, adjacentPageKey)
listStatus.postValue(AtFrontLoadMoreSuccess())
}
fun onError() {
listStatus.postValue(AtFrontLoadMoreError())
}
}
这几个回调分别对应初始化、向前加载、向后加载
为请求创建DataSource
class BourseListDataSource(status: MutableLiveData<ListStatus>) : ListStatusPageKeyedDataSource<Int, Bourse>(status) {
override fun onLoadInitial(params: LoadInitialParams<Int>, callback: ListStatusPageKeyedLoadInitialCallback<Int, Bourse>) {
RetrofitClient.getInstance().getAPI().getBourseList(0, params.requestedLoadSize).enqueue(object : Callback<HttpResponse<ListInfo<Bourse>>> {
override fun onResponse(call: Call<HttpResponse<ListInfo<Bourse>>>?, response: Response<HttpResponse<ListInfo<Bourse>>>) {
val httpResponse = HttpResponseProcess.responseProcess(response)
callback.onResult(httpResponse.data?.list
?: emptyList<Bourse>().toMutableList(), null, 1)
}
override fun onFailure(call: Call<HttpResponse<ListInfo<Bourse>>>?, throwable: Throwable) {
val errorResponse = HttpResponseProcess.responseProcess<ListInfo<Bourse>>(throwable)
callback.onError()
}
})
}
override fun onLoadAfter(params: LoadParams<Int>, callback: ListStatusPageKeyedLoadCallback<Int, Bourse>) {
RetrofitClient.getInstance().getAPI().getBourseList(params.key * params.requestedLoadSize, params.requestedLoadSize).enqueue(object : Callback<HttpResponse<ListInfo<Bourse>>> {
override fun onResponse(call: Call<HttpResponse<ListInfo<Bourse>>>?, response: Response<HttpResponse<ListInfo<Bourse>>>) {
val httpResponse = HttpResponseProcess.responseProcess(response)
callback.onResult(httpResponse.data?.list
?: emptyList<Bourse>().toMutableList(), params.key + 1)
}
override fun onFailure(call: Call<HttpResponse<ListInfo<Bourse>>>?, throwable: Throwable) {
val errorResponse = HttpResponseProcess.responseProcess<ListInfo<Bourse>>(throwable)
callback.onError()
}
})
}
override fun onLoadBefore(params: LoadParams<Int>, callback: ListStatusAtFrontPageKeyedLoadCallback<Int, Bourse>) {
}
class Factory(private val status: MutableLiveData<ListStatus>) : DataSource.Factory<Int, Bourse>() {
override fun create(): DataSource<Int, Bourse> = BourseListDataSource(status)
}
}
@FormUrlEncoded
@POST("market/getBourseList")
fun getBourseList(@Field("start") start: Int, @Field("size") size: Int): Call<HttpResponse<ListInfo<Bourse>>>
接口的DataSource中各回调直接通过Retrofit联网请求数据(没有数据持久化需求),getBourseList
api需传的参数为起始位置start与数量size, params.requestedLoadSize
就是前面PagedList.Config
中所配置PageSize
onLoadInitial
中传的start为0
onLoadAfter
中传的start为params.key * params.requestedLoadSize
(页码乘每页数量,如果api分页需要的是页码,直接传params.key就可以)
onLoadBefore
跟onLoadAfter
一样,不需要向上加载的话就置空
请求成功后将数据及下一页页码传给callback的onResult
,请求失败直接调用callback的onError
为页面创建ViewModel
class BourseListViewModel(application: Application) : MyAndroidViewModel(application) {
val listStatus = MutableLiveData<ListStatus>()
val list = makePagedList(BourseListDataSource.Factory(listStatus), listStatus)
}
ok,就这些...
页面扩展公共方法
fun Fragment.bindList(adapter: AssemblyPagedListAdapter<Any>, hint: HintView?, listStatus: MutableLiveData<ListStatus>, list: LiveData<PagedList<Any>>, emptyFragment: Fragment? = null,
isFastHidden: Boolean = false, onClickListener: View.OnClickListener? = null) {
list.observe(this, Observer { adapter.submitList(it) })
listStatus.observe(this, Observer {
when (it) {
null -> adapter.loadMoreFinished(false)
is Initialize -> hint?.loading()?.show(childFragmentManager)
is InitializeSuccess -> if (isFastHidden) hint?.fastHidden() else hint?.hidden()
is InitializeError -> hint?.error(onClickListener)?.show(childFragmentManager)
is End -> adapter.loadMoreFinished(true)
is Empty -> if (emptyFragment != null) hint?.empty(emptyFragment)?.show(childFragmentManager) else hint?.empty()?.show(childFragmentManager)
}
})
}
这个仅供参考,AssemblyPagedListAdapter
和HintView
都是自定义的,大家知道什么意思就行...
页面
class MyListFragment : BaseFragment(){
private val viewModel by bindViewModel(BourseListViewModel::class)
private val adapter = AssemblyPagedListAdapter<Any>().apply {
addItemFactory(BourseItem.Factory())
addMoreItem(LoadMoreItem.Factory())
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fm_list, container, false)
}
override fun initViews(view: View, savedInstanceState: Bundle?) {
recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
recyclerView.adapter = adapter
}
override fun loadData() {
bindList(adapter, hintView, viewModel.listStatus, viewModel.list as LiveData<PagedList<Any>>)
}
}
收工,可以看到页面其实只需要初始化View与ViewModel,然后直接bindList就可以了