如何使用分页库 android 将空视图与分页一起使用?

How to use empty view with pagination using paging library android?

只需将侦听器或回调函数添加到您的 DataSourceFactoryDataSource 并在 loadInitial 中的列表为空时调用它:

class DataSourceFactory(
    private val dataObservable: Observable<List<Data>>,
    private val onEmptyAction: () -> Unit
) : DataSource.Factory<Int, Data >() {

     override fun create(): DataSource {
         return DataSource(observable, onEmptyAction)
     }
}

class DataSource(
    private val observable: Observable<List<Data>>,
    private val onEmptyAction: () -> Unit
) : ItemKeyedDataSource<Int, Data>() {

    private val data = mutableListOf<Data>()

    override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Data>) {
        observable
            .subscribe({ data ->
                if (data.isEmpty()) {
                    // Inform someone that this list is empty from the
                    // beginning to be able to show an empty page
                    onEmptyAction()
                }

                // rest of your code & logic
        }, { Timber.e(it) })
    }
}

更新[24/04/19]: 我刚刚发现该库已经为我们提供了一种使用 PagedList.BoundaryCallback<YourItem> 来监听空初始加载的方法。

*请注意,我的旧答案仍然是一个有效的替代方案。

val livedPageList = LivePagedListBuilder(sourceFactory, config)
        .setBoundaryCallback(object: PagedList.BoundaryCallback<YourItem>() {
            override fun onZeroItemsLoaded() {
                super.onZeroItemsLoaded()
                // Handle empty initial load here
            }

            override fun onItemAtEndLoaded(itemAtEnd: YourItem) {
                super.onItemAtEndLoaded(itemAtEnd)
                // Here you can listen to last item on list
            }

            override fun onItemAtFrontLoaded(itemAtFront: YourItem) {
                super.onItemAtFrontLoaded(itemAtFront)
                // Here you can listen to first item on list
            }
        })
        .build()

原答案:

基于 class google 个样本 Network State。修改它以处理 initialLoad.

中的空内容
@Suppress("DataClassPrivateConstructor")
data class NetworkState private constructor(
    val status: Status,
    val msg: String? = null
) {

    enum class Status {
        RUNNING,
        SUCCESS_LOADED, // New
        SUCCESS_EMPTY, // New
        FAILED
    }

    companion object {

        val EMPTY = NetworkState(Status.SUCCESS_EMPTY) // New
        val LOADED = NetworkState(Status.SUCCESS_LOADED) // New
        val LOADING = NetworkState(Status.RUNNING)
        fun error(msg: String?) = NetworkState(Status.FAILED, msg)
    }
}

用法如下:

class DataSource: PageKeyedDataSource<Long, Item>() {

    val initialLoad: MutableLiveData<NetworkState> = MutableLiveData()

    override fun loadInitial(params: LoadInitialParams<Long>, callback: LoadInitialCallback<Long, Item>) {
        initialLoad.postValue(NetworkState.LOADING)
        apiCallSource.subscribe({ items ->
            if (items.isEmpty()) {
                initialLoad.postValue(NetworkState.EMPTY)
            } else {
                initialLoad.postValue(NetworkState.LOADED)
            }
        }, { error -> 
            // handle error
        })
    }
}

activity 是这样处理的:

class activity: AppCompatActivity() {

    val viewModel = // init viewmodel

    override fun onCreate(savedInstanceState: Bundle?) {
        viewModel.refreshState.observe(this, Observer { networkState ->
            if (it == NetworkState.LOADING) {
                // Show loading
            } else {
                // Hide loading

                if (it.status == NetworkState.Status.SUCCESS_EMPTY) {
                    // Show empty state for initial load
                }
            }
        }
    }
}

有关如何将 DataSourceActivity 连接的更多详细信息,请参阅此 sample

在您的 fragment/activity 中,您正在观察网络状态:

viewModel.getNetworkState1()?.observe(this, Observer {
                // here you can handle you empty view 
                setEmptyView()

            })

像这样:

private fun setNoTransactionsLayout() {
    if(viewModel.listIsEmpty()) {
        yourTextView.visibility = View.VISIBLE
    } else {
        yourTextView.visibility = View.GONE
    }
}

在视图模型中你有这个功能:

fun listIsEmpty(): Boolean {
    return yourPagedList?.value?.isEmpty() ?: true
}