android - 分页库 : skip loadInitial in PageKeyedDataSource

标签 android android-architecture-components android-jetpack android-paging

根据文档,在 UI 线程中创建 PagedList 期间调用 loadInitial 以提供一些占位符,直到获取实际数据以避免向用户显示空列表。

有什么方法可以跳过 loadInitial 或返回空列表吗?

如果我使用默认配置返回空列表,loadAfter 将不会被调用。 我查看了一些实现,但所有人都只是复制他们的代码以在 loadInitialloadAfter 中从互联网加载数据,在我看来这不是很好的解决方案。

最佳答案

在您的loadInitial 方法中只需设置重试操作

setRetry(Action { loadInitial(params, callback) })

override fun loadInitial(params: LoadInitialParams<String>, callback: LoadInitialCallback<String, Lab>) {
            setRetry(Action { loadInitial(params, callback) })

            // Set network state if you want
            networkState.postValue(Response(Response.ViewState.ERROR, null, Throwable("")))
            initialLoad.postValue(Response(Response.ViewState.ERROR, null, Throwable("")))
    }

setRetry 方法

private var retryCompletable: Completable? = null

private fun setRetry(action: Action?) {
        if (action == null) {
            this.retryCompletable = null
        } else {
            this.retryCompletable = Completable.fromAction(action)
        }
    }

当你想调用 loadAfter 时,你必须 invalidate sourceFactory

fun refresh() = sourceFactory.dataSource.value.invalidate()

关于android - 分页库 : skip loadInitial in PageKeyedDataSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53535609/

相关文章:

Android Studio - 工具栏的抽屉切换不打开抽屉

android架构组件: What is the alternative to fragment container in android architecture navigation?

android - 启动新的撰写项目时出错 java.lang.AbstractMethodError

android - 如何使用 NavController.navigate 替换 fragment 而不是将其添加到顶部

android - 如何在 Android 导航组件中管理 BottomNavigationView 后台堆栈

android - Android 中的 Flurry Analytic 集成错误

android - 如何在 ZeroMQ(jzmq) 3.xx 中使用 XPUB 和 XSUB 实现带有代理的发布-订阅网络

android - 导航组件的多个容器

Android:在应用程序中间从 3G 切换到 WIFI = 网络连接丢失

android - 如何为 AndroidViewModel 创建 View 模型工厂?