在 FirebaseUI 的 FirebaseRecyclerAdapter 中检查 "no documents" 状态或空数据集

Check for "no documents" state or empty data-set in FirebaseUI's FirebaseRecyclerAdapter

我写了一个自定义的 FirebaseRecylerAdapter,基于 FirebaseUI documentation 像这样:

class FavoritesAdapter(lifecycleOwner: LifecycleOwner) : FirebaseRecyclerAdapter<Favorite, FavoritesAdapter.FavoritesHolder>(buildOptions(lifecycleOwner)) {

companion object {
    private fun buildQuery() = FirebaseDatabase.getInstance()
            .reference
            .child("favorites")
            .limitToLast(50)

    private fun buildOptions(lifecycleOwner: LifecycleOwner) = FirebaseRecyclerOptions.Builder<Favorite>()
            .setQuery(buildQuery(), Favorite::class.java)
            .setLifecycleOwner(lifecycleOwner)
            .build()
}

//...

这个 class 覆盖了一个 onDataChanged() 函数:

override fun onDataChanged() {
    // Called each time there is a new data snapshot. You may want to use this method
    // to hide a loading spinner or check for the "no documents" state and update your UI.
    // ...

}

现在,我如何实际检查 "no documents" 状态以更新我的 UI?我找不到检查空数据集的方法。我正在寻找类似 getItemCount() 的东西。

适配器确实有一个 itemCount 属性,您也可以使用 snapshots 来获取模型对象的实时列表。示例:

override fun onDataChanged() {
    if (itemCount == 0) {
        // Do stuff
    }
}