分页库在加载初始数据时不会触发观察

Paging Library doesn't trigger observe when load initial data

我正在设置我使用协程在数据源中获取的项目的分页列表,我也在观察这个列表以在适配器中提交它,但是当它最初加载一些数据时,它不会触发观察回调。我该怎么做才能处理这个问题?

我试图调试这个东西,我发现 ArrayList> mCallbacks 列表在 PagedList 中,当它试图通知数据更改时不包含任何回调,但我不知道如何处理这个。

data 中的数据源将被提取和分页。

class PagedDataSource(private val account: Account, private val getItems: GetItems): PageKeyedDataSource<Int, Item>() {

    override fun loadInitial(
        params: LoadInitialParams<Int>,
        callback: LoadInitialCallback<Int, Transaction>
    ) {
        GlobalScope.launch {
            val startPage = 0
            account.id?.let {
                val items = getItems(it, startPage).body.toMutableList()
                callback.onResult(items, null, 1)
            }
        }
    }

    override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, Item>) {
        GlobalScope.launch {
            account.id?.let {
                val list = getItems(it, params.key)
                val items = list.body.toMutableList()
                callback.onResult(items, if (params.key >= list.totalPages) null else params.key + 1)
            }
        }
    }

    override fun loadBefore(params: LoadParams<Int>, callback: LoadCallback<Int, Item>) {
        //NO NEED
    }

}

片段中的代码:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState) 
        pagedAdapter = PagedAdapter()

        vItems.layoutManager = LinearLayoutManager(context)
        vItems.isNestedScrollingEnabled = false
        vItems.adapter = pagedAdapter

                   items.observe(viewLifecycleOwner, Observer { items ->
                if (items != null && items.isNotEmpty()) {
                   pagedAdapter.submitList(items)
                } else {
                    vItemsTitle.visibility = View.VISIBLE
                }
            })
}

最后在我的视图模型中编写代码

    init {
        items = initializedPagedList()
    }

    private fun initializedPagedList() : LiveData<PagedList<Item>> {
        val factory = object: DataSource.Factory<Int, Item>() {
            override fun create(): DataSource<Int, Item> {
                return PagedDataSource(account, getItems)
            }
        }
        val config = PagedList.Config.Builder()
            .setPageSize(20)
            .setEnablePlaceholders(false)
            .build()

        return LivePagedListBuilder(factory, config).build()
    }

我希望在成功 api 调用 loadInitial 方法并触发观察回调后获取数据。

经过研究,我终于找到了问题的答案。


问题出在这段代码中

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState) 
    pagedAdapter = PagedAdapter()

    vItems.layoutManager = LinearLayoutManager(context)
    vItems.isNestedScrollingEnabled = false
    vItems.adapter = pagedAdapter

               items.observe(viewLifecycleOwner, Observer { items ->
            if (items != null && items.isNotEmpty()) {
               pagedAdapter.submitList(items)
            } else {
                vItemsTitle.visibility = View.VISIBLE
            }
        })
 }

我需要将其更改为

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState) 
    pagedAdapter = PagedAdapter()

    vItems.layoutManager = LinearLayoutManager(context)
    vItems.isNestedScrollingEnabled = false
    vItems.adapter = pagedAdapter

               items.observe(viewLifecycleOwner, Observer { items ->
               pagedAdapter.submitList(items)
        })
 }

我认为发生这种情况是因为在前台 PagedList 异步工作/并且您需要提交此列表一次,然后数据源将更新直接发送到适配器,避免观察。在我的例子中,如果列表不为空,我将提交,但是当你在开始时创建 PagedList 时它总是空的。

祝大家好运!