gpt4 book ai didi

android - 缺少使用分页查看的回调?

转载 作者:行者123 更新时间:2023-12-02 13:00:23 25 4
gpt4 key购买 nike

我正在尝试进行分页并使用 Google 的新库,但看到了一些奇怪的行为。我不确定我哪里出错了。

我是followig MVP,并且还使用了一些 Dagger 注入(inject)来进行可测试性。

在 View 中:

val adapter = ItemsAdapter()
viewModel.getItems(itemCategoryId, keywords).observe(this, Observer {
Log.d(TAG, "Items updated $it")
adapter.setList(it)
})

数据源工厂:
class ItemDataSourceFactory(
private val api: Api,
private val retryExecutor: Executor
) : DataSource.Factory<Long, Item> {

private val mutableLiveData = MutableLiveData<ItemDataSource>()

override fun create(): DataSource<Long, Item> {
val source = ItemDataSource(api, retryExecutor)
mutableLiveData.postValue(source)
return source
}
}

数据来源:
class ItemDataSource(
private val api: Api,
private val retryExecutor: Executor
): ItemKeyedDataSource<Long, Item>() {

companion object {
private val TAG = ItemKeyDataSource::class.java
}

override fun getKey(item: Item): Long = item.id

override fun loadBefore(params: LoadParams<Long>, callback: LoadCallback<Item>) {
// ignored, since we only ever append to our initial load
}

override fun loadInitial(params: LoadInitialParams<Long>, callback: LoadInitialCallback<Item>) {
api.loadItems(1, params.requestedLoadSize)
.subscribe({
Logger.d(TAG, "Page 1 loaded. Count ${params.requestedLoadSize}.\nItems: ${it.items}")
callback.onResult(it.items as MutableList<Item>, 0, it.item.size)
}, {})
}

override fun loadAfter(params: LoadParams<Long>, callback: LoadCallback<Item>) {
api.loadItems(params.key, params.requestedLoadSize)
.subscribe({
Logger.d(TAG, "Page ${params.key} loaded. Count ${params.requestedLoadSize}.\nItems: ${it.items}")
callback.onResult(it.itemsas MutableList<Item>)
}, {})
}
}

和 View 模型:
  class ItemsViewModel @Inject internal constructor(
private val repository: ItemsMvp.Repository
): ViewModel(), ItemsMvp.Model {

override fun items(categoryId: Long, keywords: String?): LiveData<PagedList<Item>> {
return repository.items(categoryId, keywords)
}
}

和存储库层:
class ItemsRepository @Inject internal constructor(
private val api: Api,
) : ItemsMvp.Repository {

companion object {
const val DEFAULT_THREAD_POOL_SIZE = 5
const val DEFAULT_PAGE_SIZE = 20
}

private val networkExecutor = Executors.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE)
private val pagedListConfig = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setInitialLoadSizeHint(DEFAULT_PAGE_SIZE)
.setPageSize(DEFAULT_PAGE_SIZE)
.build()

override fun items(categoryId: Long, keywords: String?): LiveData<PagedList<Item>> {
val sourceFactory = ItemDataSourceFactory(api, networkExecutor)

// provide custom executor for network requests, otherwise it will default to
// Arch Components' IO pool which is also used for disk access
return LivePagedListBuilder(sourceFactory, pagedListConfig)
.setBackgroundThreadExecutor(networkExecutor)
.build()
}
}

问题是加载第一页后我没有更新 View 。

我从 onCreate() 中看到了这个日志:
 Items updated []

但是在数据源返回项目之后,我看到了这些日志:
Page 1 loaded. Count 20.
Items: [Item(....)]

但是 我从来没有看到订阅 View 模型的 View 获得更新以在适配器上设置列表。如果你好奇我正在使用 PagedListAdapter。

最佳答案

我有两个错误...

  • 从未在 View 中设置适配器(失败)
  • 我正在扩展 ItemKeyedDataSource 而不是 PageKeyedDataSource(双重失败)

  • 我做了这两个调整,现在一切都按预期运行。

    关于android - 缺少使用分页查看的回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49001879/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com