【问题标题】:How to use empty view with pagination using paging library android?如何使用分页库android使用空视图和分页?
【发布时间】:2020-05-17 09:10:34
【问题描述】:
  • 如何在设置适配器之前确定返回数据的大小?
  • 如何在分页库中使用emptyview?
  • pagedlist返回null或无数据时如何设置emptyview?

【问题讨论】:

标签: android kotlin paging mvp


【解决方案1】:

更新[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()

原答案:

基于 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
        })
    }
}

这就是活动处理它的方式:

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

【讨论】:

  • 你能展示如何从 DataSource 获取 initialLoad 变量到 viewmodel
  • onZeroItemsLoaded 永远不会被调用
【解决方案2】:

只需将侦听器或回调函数添加到您的 DataSourceFactory 和您的 DataSource 并在 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) })
    }
}

【讨论】:

    【解决方案3】:

    在您的片段/活动中,您正在观察网络状态:

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-12
      • 2016-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-26
      • 2020-05-25
      相关资源
      最近更新 更多