android - Android - 从 Firebase Storage 获取所有下载链接后无法更新 RecyclerView
问题描述
我想从 Firebase 存储中获取所有视频文件并将它们显示在 RecyclerView 中。我已经设法获取所有文件,但是一旦检索到所有文件,我就无法更新 RecyclerView。这是代码
recyclerViewVideoList.setHasFixedSize(true)
recyclerViewVideoList.layoutManager = LinearLayoutManager(this)
recyclerViewVideoList.adapter = VideoListRecyclerViewAdapter(applicationContext,videoList, this)
getVideos()
private fun getVideos() {
val listRef = firebaseStorage.reference.child("videos")
listRef.listAll()
.addOnSuccessListener { listResult ->
listResult.items.forEach { item ->
item.downloadUrl.addOnSuccessListener {
videoList.add(Video(item.name, it.toString(), "565656"))
}
}
recyclerViewVideoList.adapter!!.notifyDataSetChanged()
}
.addOnFailureListener {
Toast.makeText(applicationContext, "Something went wrong. Please try again", Toast.LENGTH_SHORT).show()
}
}
这里有一个解决方案,但它多次调用 notifyDataSetChanged() 。我想避免这样做。
解决方案
更有效的解决方案是仅对列表项的最后一个元素调用notifyDataSetChanged()函数。通过这样做,您的 notifyDataSetChanged() 函数仅针对最后一个元素执行并更新所有回收站视图。
改变这个:
.addOnSuccessListener { listResult ->
listResult.items.forEach { item ->
item.downloadUrl.addOnSuccessListener {
videoList.add(Video(item.name, it.toString(), "565656"))
}
}
recyclerViewVideoList.adapter!!.notifyDataSetChanged()
}
这
//Declare one integer count variable var count = 0
.addOnSuccessListener { listResult ->
listResult.items.forEach { item ->
item.downloadUrl.addOnSuccessListener {
videoList.add(Video(item.name, it.toString(), "565656"))
count++
if(count == listResult.items.size)
recyclerViewVideoList.adapter!!.notifyDataSetChanged()
}
}
}
推荐阅读
- android-glide - 我正在使用 glide 从 firebase 检索图像。我想在单击时在新活动中显示该特定图像
- python - 使用索引访问列表时计数不起作用
- reactjs - 我想在反应映射函数中运行 if else 条件。这个怎么做?
- python - python qt中的自定义Qprogressbar
- reactjs - NextJS 关于 slugs 的动态路由问题
- esp32 - 使用Heltec WIFI LoRa 32 (V2)和模组Sim800A时GPRS连接网络失败
- sql - 如何找出并打印不匹配记录的总和?
- javascript - 在购物车中隐藏特定产品 ID 的“确认”支付网关
- python - 使用 Chromedriver 遍历多个页面
- javascript - 如何在构建新的 Nuxt 应用程序时推出生产