0

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

  • Since you're using Kotlin, have you read this [resource](https://medium.com/firebase-tips-tricks/how-to-paginate-firestore-using-paging-3-on-android-c485acb0a2df) and this [resource](https://medium.com/firebase-tips-tricks/how-to-implement-pagination-in-firestore-using-jetpack-compose-76b4c0b5acd5)? – Alex Mamo Oct 25 '22 at 18:58
  • Please edit your question and add your database structure as a JSON file. You can simply get it by clicking the Export JSON in the overflow menu (⠇) in your [Firebase Console](https://console.firebase.google.com/u/0/project/_/database/data). – Alex Mamo Oct 30 '22 at 11:32

2 Answers2

0

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:

    auth= FirebaseAuth.getInstance()
    //binding.idRLWallpapers.adapter= ImageAdp(arrayList)
    adapters= ImageAdp(ArrayList()) // like this
    binding.idRLWallpapers.adapter=adapters
    getUsers(null)

Turn your arrayList to local list to become empty for every 10 item.

Query.addValueEventListener(object : ValueEventListener {
      override fun onDataChange(snapshot: DataSnapshot) {
      var arrayList=ArrayList<imagemodel>() // like this

Delete below 2 lines to prevent recreate:

  //adapters= ImageAdp(arrayList) // You did this in OnCreate
  //binding.idRLWallpapers.adapter=adapters // remove these 2 lines 
  //inside Query.addValueEventListener
  adapters.addAll(arrayList)

These should work, if not i am gonna need more of your code to understand it properly.

Mert
  • 904
  • 4
  • 9
  • 21
  • With this code you share, now everything seems fine, try to analyze the other parts of the code, Check if adapter class has another refreshing method like notifyDataSetChanged(), and check where you control scrolling maybe it is scrolling continuously and recyclerview keeps refreshing. Other than that i can not say anything more. – Mert Oct 19 '22 at 21:10
  • Maybe but i can not comment without seeing its behavior. Check this two links similar to your problem: [first](https://stackoverflow.com/questions/30236574/android-how-to-stop-images-reloading-in-recyclerview-when-scrolling) and [second](https://stackoverflow.com/questions/28658579/refreshing-data-in-recyclerview-and-keeping-its-scroll-position) – Mert Oct 20 '22 at 09:05
  • I solved this problem. Your code is working fine as long as I use constraint layout. I solved it by giving the dimensions to recyclerview row the problem was gone. – MehmetEminKisbet Nov 03 '22 at 18:31
  • kod düzgün bir şekilde çalışıyor lakin başka bir aktiviteye gittiğimde ve geri pagination yaptığım aktiviteye gelince en başa atıyor normalde olması gereken yere atmıyor belki anlatamamış olabilirim burda bunu şöyle örnekleyeyim instagramda postlarda birkaç post aşağıya kaydırıyosun ve başka aktiviteden buraya döndüğünde altta olman gerekirken seni en üste atıyor sence bunu nasıl çözebilirim? – MehmetEminKisbet Nov 07 '22 at 14:42
  • It's normal behavior. Because when you left the activity, activity destroys and when you come back it recreates again. So your old data lost and it will do all thing from beginning. Instagram like apps uses advanced methods such as mvvm, livedata etc. They get the data only 1 time and observe changes, and update only changes not all. Aside that i can not think another solution, but maybe there is someone knows better. If you stuck just ask that in seperate question. – Mert Nov 07 '22 at 18:45
0

Declare your array list here .Then Add it to adapter

    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()


    }
monjur
  • 19
  • 7