如何使用分页库Android在分页中使用空视图?

时间:2018-11-15 05:34:52

标签: android kotlin paging mvp

  • 在设置适配器之前如何确定返回的数据大小?
  • 如何将Emptyview与分页库一起使用?
  • 如果pagedlist返回null或无数据,如何设置emptyview?

3 个答案:

答案 0 :(得分:4)

更新[19年4月24日]: 我刚刚发现该库已经为我们提供了一种使用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
                }
            }
        }
    }
}

有关如何将 DataSource Activity 连接的更多详细信息,请参见此sample

答案 1 :(得分:1)

只要在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) })
    }
}

答案 2 :(得分:0)

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

v.iter()

像这样:

let v = vec![100, 32, 57];
for i in &v { // borrow, not move
    println!("{}", i);
}
println!("{}", v[0]); // if v is moved above, this doesn't work

在视图模型中,您具有以下功能:

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

            })