我想从Firebase Storage获取所有视频文件,并将其显示在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()
}
}
}
更改此项:
.addOnSuccessListener { listResult ->
listResult.items.forEach { item ->
item.downloadUrl.addOnSuccessListener {
videoList.add(Video(item.name, it.toString(), "565656"))
}
}
recyclerViewVideoList.adapter!!.notifyDataSetChanged()
}
进入这个:
.addOnSuccessListener { listResult ->
listResult.items.forEach { item ->
item.downloadUrl.addOnSuccessListener {
videoList.add(Video(item.name, it.toString(), "565656"))
recyclerViewVideoList.adapter!!.notifyDataSetChanged()
}
}