在FirebaseUI的FirebaseRecyclerAdapter

时间:2018-04-19 05:37:07

标签: android firebase firebase-realtime-database kotlin firebaseui

我写了一个基于FirebaseUI documentation的自定义FirebaseRecylerAdapter,如下所示:

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()
}

//...

该类重写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.
    // ...

}

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

1 个答案:

答案 0 :(得分:1)

适配器确实具有itemCount属性,您还可以使用snapshots获取模型对象的实时列表。例如:

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