This is my OnCreateActivity(I didn’t put any more to avoid confusion)…………………………………………………………………………………………………………….
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
}
}
})
This is how i am fetching data from firebase database .
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)
This is my RecyclerView Adapter(I didn’t put any more to avoid confusion)
fun addAll(emp:List<imagemodel>){
var initialSize= myarraylist.size
myarraylist.addAll(emp)
notifyDataSetChanged()
}
fun lastItemId(): String? {
return myarraylist.get(myarraylist.size-1).uid
}
Thank you for your time
2
Answers
As i understand, you are initializing your adapter in OnCreate and trying to initialize again for every 10 element in down below, and last one replaces the previous one.
Initialize adapter with empty arraylist in OnCreate:
Turn your arrayList to local list to become empty for every 10 item.
Delete below 2 lines to prevent recreate:
These should work, if not i am gonna need more of your code to understand it properly.
Declare your array list here .Then Add it to adapter