这是我的OnCreateActivity(我没有放更多以避免混淆)……………………………………………………………………………………………………………………………
auth= FirebaseAuth.getInstance()
adapters= ImageAdp(ArrayList())
binding.idRLWallpapers.adapter=adapters
getUsers(null)
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
var linearLayoutManager:LinearLayoutManager= recyclerView.layoutManager as LinearLayoutManager
var currentItems=linearLayoutManager.childCount
total_item=linearLayoutManager.itemCount
var lastVisibleitem=linearLayoutManager.findFirstVisibleItemPosition()
if(!isLoadingEDMT&&total_item<=lastVisibleitem+ITEM_COUNT){
// misLoad=true
getUsers(adapters.lastItemId())
isLoadingEDMT=true
binding.bottomPB.visibility=View.GONE
isLoad=false
}
}
})
这就是我从Firebase数据库获取数据的方式。
private fun getUsers(nodeId:String?) {
if (nodeId==null){
Query = database.orderByKey().limitToFirst(ITEM_COUNT)
}
else{
Query = database.orderByKey().startAfter(nodeId).limitToFirst(ITEM_COUNT)
}
Query.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
var arrayLists=ArrayList<imagemodel>()
if (snapshot.hasChildren()) {
for (data in snapshot.children) {
val image = data.child("url2").value.toString()
val name = data.child("voice1").value.toString()
val childname = data.child("childname").value.toString()
val id = data.child("id").value.toString()
val model = imagemodel(image, name,
arrayLists.add(model)
}
//adapters= ImageAdp(arrayList)
//binding.idRLWallpapers.adapter=adapters
adapters.addAll(arrayLists)
这是我的回收视图适配器(为了避免混淆,我没有再放了)
fun addAll(emp:List<imagemodel>){
var initialSize= myarraylist.size
myarraylist.addAll(emp)
notifyDataSetChanged()
}
fun lastItemId(): String? {
return myarraylist.get(myarraylist.size-1).uid
}
谢谢你抽出时间
据我所知,您正在OnCreate中初始化适配器,并尝试为下面的每10个元素再次初始化,最后一个替换前一个。
在OnCreate中使用空数组列表初始化适配器:
auth= FirebaseAuth.getInstance()
//binding.idRLWallpapers.adapter= ImageAdp(arrayList)
adapters= ImageAdp(ArrayList()) // like this
binding.idRLWallpapers.adapter=adapters
getUsers(null)
将您的arrayList转换为本地列表,每10个项目变为空。
Query.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
var arrayList=ArrayList<imagemodel>() // like this
删除2行以下以防止重新创建:
//adapters= ImageAdp(arrayList) // You did this in OnCreate
//binding.idRLWallpapers.adapter=adapters // remove these 2 lines
//inside Query.addValueEventListener
adapters.addAll(arrayList)
这些应该可以工作,如果不行,我需要更多的代码来正确理解它。
在此处声明您的数组列表。然后将其添加到适配器
Query.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
var arrayList=ArrayList<imagemodel>()
//your codes as it is
if (snapshot.hasChildren()) {
for (data in snapshot.children) {
val image = data.child("url2").value.toString()
val name = data.child("voice1").value.toString()
val childname =
data.child("childname").value.toString()
val id = data.child("id").value.toString()
val model = imagemodel(image, name, childname, id)
arrayList.add(model)
}
}
adapter.addAll(arrayList)
adapter.notyfyDataSetHasChanged()
}