You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Kotlin 从Firebase Realtime Database拉取数据时如何给RecyclerView加加载效果

实现Firebase数据加载状态的方案

步骤1:修改Fragment布局文件

在你的fragment_day_detail.xml中新增全局加载控件,同时调整原有控件的默认可见性:

<!-- 根布局下新增ProgressBar作为加载指示器 -->
<ProgressBar
    android:id="@+id/progressLoading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:visibility="visible" />

<!-- 原有RecyclerView和空状态布局lrDbEmpty的默认visibility改为gone -->
<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone" />

<LinearLayout
    android:id="@+id/lrDbEmpty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone"
    ...>
    <!-- 空状态原有内容 -->
</LinearLayout>

步骤2:修改Fragment的读数据逻辑

调整readFromDatabase方法,在请求发起时显示加载状态,回调返回后隐藏加载,再根据结果显示对应布局:

private fun readFromDatabase() {
    // 发起请求前先显示加载状态
    binding.apply {
        progressLoading.visibility = View.VISIBLE
        lrDbEmpty.visibility = View.GONE
        recyclerView.visibility = View.GONE
    }

    ref?.addValueEventListener(object: ValueEventListener {
        override fun onDataChange(snapshot: DataSnapshot) {
            // 拿到响应后先隐藏加载
            binding.progressLoading.visibility = View.GONE
            if (snapshot.exists()) {
                val list = ArrayList<Day>()
                for (daySnapshot in snapshot.children) {
                    val day = daySnapshot.getValue(Day::class.java)
                    // 替换原有强制拆包,避免空数据崩溃
                    day?.let { list.add(it) }
                }
                if (list.isEmpty()) {
                    // 解析后数据为空,显示空状态
                    binding.apply {
                        lrDbEmpty.visibility = View.VISIBLE
                        recyclerView.visibility = View.GONE
                    }
                } else {
                    // 数据正常,显示列表
                    adapter.submitList(list)
                    binding.apply {
                        recyclerView.visibility = View.VISIBLE
                        lrDbEmpty.visibility = View.GONE
                    }
                }
            } else {
                // 无数据返回,显示空状态
                binding.apply {
                    lrDbEmpty.visibility = View.VISIBLE
                    recyclerView.visibility = View.GONE
                }
            }
        }

        override fun onCancelled(error: DatabaseError) {
            // 请求失败,隐藏加载,显示空状态(也可自定义错误提示)
            binding.progressLoading.visibility = View.GONE
            binding.lrDbEmpty.visibility = View.VISIBLE
        }
    })
}

可选优化

如果需要实现上拉加载更多的加载状态,才需要修改Adapter新增加载更多的item类型,你当前需要的首次加载全局提示按上述两步修改即可。

内容的提问来源于stack exchange,提问作者onesector

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 02:06:05