小编Hos*_*ian的帖子

分页库 Callback.OnResult 不返回 Livedata 的值

我想实现 jetpack 分页库。

在从 api(使用 Retrofit2 和协程)获得响应后,一切似乎都在工作,android 分页回调正在调用他的 onResult 接口。

但是当接收到数据时,它并没有将它的值返回给主要活动中的 livedata。

解决办法是什么 ?

视图模型

    fun getListData(): LiveData<PagedList<DataListModel>> {
        return initializedPagedListBuilder(PagingConfig.config).build()
    }


    private fun initializedPagedListBuilder(config: PagedList.Config): LivePagedListBuilder<Int, DataListModel> {

        val dataSourceFactory = object : DataSource.Factory<Int, DataListModel>() {
            override fun create(): DataSource<Int, DataListModel> {
                return ListDataPageSource()
            }
        }
        return LivePagedListBuilder<Int, DataListModel>(dataSourceFactory, config)
    }
Run Code Online (Sandbox Code Playgroud)

列表数据页源

class ListDataPageSource : PageKeyedDataSource<Int, DataListModel>() {

    override fun loadInitial(
        params: LoadInitialParams<Int>,
        callback: LoadInitialCallback<Int, DataListModel>
    ) {
        val apiCall = RetrofitApi.getApiList("http://jsonplaceholder.typicode.com/").create(
            MoviesApi::class.java
        )

        CoroutineScope(Dispatchers.IO).launch {
            val response = …
Run Code Online (Sandbox Code Playgroud)

android android-paging android-paging-library

7
推荐指数
0
解决办法
552
查看次数