Android 如何在PagedList LiveData异步提交结果时观察它?
在下面的代码片段中,页面列表LiveData只观察了一次。它成功地将下一页加载到recyclerView。正如我们所知,它在内部使用异步调用将结果提交给适配器 问题是如何为插入列表的每个新数据观察PagedList LiveData?Android 如何在PagedList LiveData异步提交结果时观察它?,android,kotlin,android-livedata,android-paging,Android,Kotlin,Android Livedata,Android Paging,在下面的代码片段中,页面列表LiveData只观察了一次。它成功地将下一页加载到recyclerView。正如我们所知,它在内部使用异步调用将结果提交给适配器 问题是如何为插入列表的每个新数据观察PagedList LiveData? val callback = PostListAdapter.PagerCallback() viewModel.posts.observe(viewLifecycleOwner, Observer<PagedList<PostData>>
val callback = PostListAdapter.PagerCallback()
viewModel.posts.observe(viewLifecycleOwner, Observer<PagedList<PostData>> { pagedList ->
adapter.submitList(pagedList) //
adapter.updatePostList(pagedList) // I want to update this list on new data
pagedList.addWeakCallback(null, callback) // callback working fine.
}
我为我的场景找到了解决方案,并分享了相同的解决方案 我们无法在第一次调用后观察PagedList LiveData,因为它将结果异步提交给适配器,这就是为什么LiveData不会触发的原因 解决方案:我添加了新的LiveData,并在从服务器获得响应后在DataSource类中更新它 视图模型
viewModel.posts.observe(viewLifecycleOwner, Observer { pagedList ->
adapter.submitList(pagedList)
})
// Added new LiveData to observer my list and pass it to detail screen
viewModel.postList.observe(viewLifecycleOwner, Observer {
adapter.updatePostList(it)
})
存储库
class MyRepository(private val api: ApiService) : {
val sourceFactory = MyDataSourceFactory(api) // create MyDataSource
val postList = Transformations.switchMap( // this is new live data to observe list
sourceFactory.sourceLiveData) { it.postList }
val data = LivePagedListBuilder(sourceFactory, PAGE_SIZE).build()
return Result(data, postList)
}
class MyDataSourceFactory(private val api: ApiService) :
DataSource.Factory<String, PostData>() {
val sourceLiveData = MutableLiveData<MyDataSource>()
override fun create(): DataSource<String, PostData> {
val source = MyDataSource(api)
sourceLiveData.postValue(source)
return source
}
}
数据源工厂
class MyRepository(private val api: ApiService) : {
val sourceFactory = MyDataSourceFactory(api) // create MyDataSource
val postList = Transformations.switchMap( // this is new live data to observe list
sourceFactory.sourceLiveData) { it.postList }
val data = LivePagedListBuilder(sourceFactory, PAGE_SIZE).build()
return Result(data, postList)
}
class MyDataSourceFactory(private val api: ApiService) :
DataSource.Factory<String, PostData>() {
val sourceLiveData = MutableLiveData<MyDataSource>()
override fun create(): DataSource<String, PostData> {
val source = MyDataSource(api)
sourceLiveData.postValue(source)
return source
}
}
class MyDataSourceFactory(私有val api:ApiService):
DataSource.Factory(){
val sourceLiveData=MutableLiveData()
重写fun create():数据源{
val source=MyDataSource(api)
sourceLiveData.postValue(源)
返回源
}
}
数据源
class MyDataSource(private val api: ApiService)
:PageKeyedDataSource<String, PostData>() {
val postList = MutableLiveData<List<PostData>>()
override fun loadInitial(
params: LoadInitialParams<String>,
callback: LoadInitialCallback<String, PostData>) {
//Api call here
val list = response.body()?.data
callback.onResult( // this line submit list to PagedList asynchronously
list,
response.body()?.data?.before,
response.body()?.data?.after)
postList.value = response.body().data // your LiveData to observe list
}
override fun loadAfter(
params: LoadParams<String>,
callback: LoadCallback<String, PostData>) {
//Api call here
val list = response.body()?.data?
callback.onResult( // this line submit list to PagedList asynchronously
list,
response.body()?.data?.before,
response.body()?.data?.after)
postList.value = response.body().data // your LiveData to observe list
}
class MyDataSource(私有val api:ApiService)
:PageKeyedDataSource(){
val postList=MutableLiveData()
覆盖趣味加载初始值(
params:LoadInitialParams,
回调:LoadInitialCallback){
//这里的Api调用
val list=response.body()?.data
onResult(//此行异步将列表提交到PagedList
列表
response.body()?.data?.before,
response.body()?.data?.after)
postList.value=response.body()
}
覆盖乐趣加载后(
参数:LoadParams,
回调:LoadCallback){
//这里的Api调用
val list=response.body()?.data?
onResult(//此行异步将列表提交到PagedList
列表
response.body()?.data?.before,
response.body()?.data?.after)
postList.value=response.body()
}