从 Firebase android kotlin 获取数据时如何显示加载的项目
Posted
技术标签:
【中文标题】从 Firebase android kotlin 获取数据时如何显示加载的项目【英文标题】:How to display items loading while data is being fetched from Firebase android kotlin 【发布时间】:2022-01-07 08:49:37 【问题描述】:我有一个RecyclerView
和我的Fragment
。我正在从 Firebase Realtime Database
获取数据到 RecyclerView。
我需要这样做,以便在加载数据时,我看到某种加载效果。我该怎么做?
代码来自我的Fragment:
private var _binding: FragmentDayDetailBinding? = null
private val binding get() = _binding!!
private var ref: DatabaseReference? = null
private lateinit var adapter: DayDetailAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View
_binding = FragmentDayDetailBinding.inflate(inflater, container, false)
setupRecyclerView()
initDatabase()
return binding.root
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
super.onViewCreated(view, savedInstanceState)
readFromDatabase()
private fun setupRecyclerView()
adapter = DayDetailAdapter()
binding.recyclerView.layoutManager = LinearLayoutManager(requireContext())
binding.recyclerView.adapter = adapter
private fun initDatabase()
FirebaseApp.initializeApp(requireContext())
ref = FirebaseDatabase.getInstance()
.getReference("IMIT")
.child("groups")
private fun readFromDatabase()
ref?.addValueEventListener(object: ValueEventListener
override fun onDataChange(snapshot: DataSnapshot)
if (snapshot.exists())
val list = ArrayList<Day>()
for (daySnapshot in snapshot.children)
val day = daySnapshot.getValue(Day::class.java)
list.add(day!!)
adapter.submitList(list)
else
binding.apply
lrDbEmpty.visibility = View.VISIBLE
recyclerView.visibility = View.INVISIBLE
override fun onCancelled(error: DatabaseError)
)
override fun onDestroyView()
super.onDestroyView()
_binding = null
来自RecyclerView Adapter:
的代码
class ViewHolder(private val binding: ItemSubjectDetailBinding): RecyclerView.ViewHolder(binding.root)
fun bind(day: Day) = with(binding)
tvSubject.text = day.subject
tvInfo.text = "$day.teacher, $day.type"
tvTime.text = day.time
tvAud.text = day.clas-s-room
companion object
fun from(parent: ViewGroup): ViewHolder
val layoutInflater = LayoutInflater.from(parent.context)
val binding = ItemSubjectDetailBinding.inflate(layoutInflater, parent, false)
return ViewHolder(binding)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder
return ViewHolder.from(parent)
override fun onBindViewHolder(holder: ViewHolder, position: Int)
with(holder)
bind(getItem(position))
class ItemComparator: DiffUtil.ItemCallback<Day>()
override fun areItemsTheSame(oldItem: Day, newItem: Day): Boolean
return oldItem == newItem
override fun areContentsTheSame(oldItem: Day, newItem: Day): Boolean
return oldItem == newItem
【问题讨论】:
这段代码中到底有什么不符合您的预期?告诉我们共享代码有什么问题。你有什么错误吗? 我也认为repo 可能会有所帮助。 @AlexMamo 哇,这个 repo 对我有很大帮助,谢谢 【参考方案1】:你已经走了一半
在片段中心创建一个progressBar
<ProgressBar
android:id="@+id/progress_bar"
android:layout_
android:layout_
/>
在获取数据(onDataChange
调用)之前保持可见,然后隐藏进度条
override fun onDataChange(snapshot: DataSnapshot)
binding.progressBar.visibility = View.GONE
if (snapshot.exists())
// rest of your code
【讨论】:
以上是关于从 Firebase android kotlin 获取数据时如何显示加载的项目的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Firebase.auth.currentUser Android Kotlin 获取刷新令牌
如何使用 Kotlin 在 Android 中处理从 Firebase Remote Config 解析 JSON 的效果方式?
如何从 Firebase android kotlin 获取所有具有特定价值的孩子?
如何在 Android Studio 中使用 Kotlin 从 Firebase 中的数据库中获取特定值?