首页 > 解决方案 > 修改 Android Paging Architecture 库中的 PagedList

问题描述

我目前正在考虑将分页架构库(2.1.0-beta01撰写本文时的版本)合并到我的应用程序中。一个组件是允许用户从中删除单个项目的列表。此列表仅限网络,使用 Room 进行本地缓存没有意义。

PagedList是不可变的,不支持修改。我已经读过,拥有一份列表副本,然后修改并作为新列表返回是要走的路。文档声明相同:

如果您有更精细的更新信号,例如网络 API 发出对列表中单个项目的更新信号,建议将数据从网络加载到内存中。然后通过包装内存快照的 DataSource 将该数据呈现给 PagedList。每次内存中的副本更改时,都会使先前的 DataSource 无效,并且可以创建一个包装快照新状态的新数据源。

我目前有基本的推荐实现来显示一个简单的列表。我的DataSource样子是这样的:

class MyDataSource<SomeItem> : PageKeyedDataSource<Int, SomeItem>() {

    override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, SomeItem>) {
        // Simple load from API and notification of `callback`.
    }

    override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, SomeItem>) {
        // Simple load from API and notification of `callback`.
    }

    override fun loadBefore(params: LoadParams<Int>, callback: LoadCallback<Int, SomeItem>) {
        // Simple load from API and notification of `callback`.
    }
}

文档中引用的内存缓存(没有 Room 并且没有使整个数据集失效)的具体实现是什么样的?

标签: androidkotlinandroid-architecture-componentsandroid-paging

解决方案


如果你想修改你的列表而不一直到数据层,你需要submitList在你的适配器中覆盖,然后在你的PagedList对象上设置一个回调。每当PagedList发生更改时,您都可以将这些更改复制到本地数据集。不建议这样做,但这是一个非常简单的 hack 开始工作。

这是一个例子:

class MyListAdapter : PagedListAdapter<MyDataItem, MyViewHolder>(MyDiffCallback()) {

    /**
     * This data set is a bit of a hack -- we are copying everything the PagedList loads into our
     * own list.  That way we can modify it.  The docs say you should go all the way down to the
     * data source, modify it there, and then bubble back up, but I don't think that will actually
     * work for us when the changes are coming from the UI itself.
     */
    private val dataSet = arrayListOf<MyDataItem>()

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        //Forces the next page to load when we reach the bottom of the list
        getItem(position)

        dataSet.getOrNull(position)?.let {
            holder.populateFrom(it)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = parent.inflate(R.layout.my_view_holder)
        return MyViewHolder(view)
    }

    class MyDiffCallback : DiffUtil.ItemCallback<MyDataItem>() {

        override fun areItemsTheSame(oldItem: MyDataItem, newItem: MyDataItem) =
                oldItem.id == newItem.id

        override fun areContentsTheSame(oldItem: MyDataItem, newItem: MyDataItem) =
                oldItem == newItem
    }

    override fun submitList(pagedList: PagedList<MyDataItem>?) {
        pagedList?.addWeakCallback(listOf(), object : PagedList.Callback() {
            override fun onChanged(position: Int, count: Int) {
                dataSet.clear()
                dataSet.addAll(pagedList)
            }

            override fun onInserted(position: Int, count: Int) {
                dataSet.clear()
                dataSet.addAll(pagedList)
            }

            override fun onRemoved(position: Int, count: Int) {
                dataSet.clear()
                dataSet.addAll(pagedList)
            }
        })
        super.submitList(pagedList)
    }
}

推荐阅读