0

I want to show Notes using recyclerView from firestore. First i will show 5 notes then When i will scroll down it has to load next 5 Notes. In my below codes i can only show first 5 Notes but next 5 Notes are not loading. when i scroll down it loads same values. Please click here to check . Please Help me.

class NewLearning : AppCompatActivity() {

    private var mAdapter: NoteRecyclerViewAdapter? = null

    private var firestoreDB: FirebaseFirestore? = null
    private var firestoreListener: ListenerRegistration? = null

    private var lastVisible: DocumentSnapshot? = null
    private var isFirstPageFirstLoad: Boolean = true

    lateinit var layoutManager : LinearLayoutManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.t_tips_learning)

        setSupportActionBar(toolbar)
        val actionBar = supportActionBar
        actionBar!!.title = "Notes"
        actionBar.setDisplayHomeAsUpEnabled(true)

        layoutManager = LinearLayoutManager(this)
        rvNoteList.layoutManager = layoutManager
        rvNoteList.adapter = mAdapter

        firestoreDB = FirebaseFirestore.getInstance()

    rvNoteList.addOnScrollListener(object : RecyclerView.OnScrollListener() {
        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)
            val reachedBottom = !recyclerView.canScrollVertically(1)
            if (reachedBottom) {
                loadMorePosts()
            }
        }

    })

       firestoreListener = firestoreDB!!.collection("Notes")
            .orderBy("date", Query.Direction.DESCENDING)
            .limit(5)
            .addSnapshotListener(EventListener { documentSnapshots, e ->
                if (e != null) {
                    Toast.makeText( this, "Something went wrong! Please try later.", Toast.LENGTH_LONG ).show()
                    return@EventListener
                }

                val notesList = mutableListOf<Note>()
                if (documentSnapshots != null) {

                    if (isFirstPageFirstLoad) {
                        lastVisible = documentSnapshots.documents[documentSnapshots.size() - 1]
                    }

                    for (doc in documentSnapshots) {
                        val note = doc.toObject<Note>(Note::class.java)
                        note.id = doc.id

                        if (isFirstPageFirstLoad) {
                            notesList.add(note)
                            mAdapter?.notifyItemInserted(notesList.size)

                        } else {
                            notesList.add(0, note)
                            mAdapter?.notifyItemInserted(0)
                        }
                    }
                    mAdapter = NoteRecyclerViewAdapter(notesList, applicationContext, firestoreDB!!)
                }
                mAdapter = NoteRecyclerViewAdapter(notesList, applicationContext, firestoreDB!!)
                rvNoteList.adapter = mAdapter

                isFirstPageFirstLoad = false
            })
        }

    private fun loadMorePosts(){

        firestoreListener = firestoreDB!!.collection("Notes")
            .orderBy("date", Query.Direction.DESCENDING)
            .startAfter(lastVisible)
            .limit(5)
            .addSnapshotListener(EventListener { documentSnapshots, e ->
                if (e != null) {
                    Toast.makeText( this, "Something went wrong! Please try later.", Toast.LENGTH_LONG ).show()
                    return@EventListener
                }

                val notesList = mutableListOf<Note>()
                if (documentSnapshots != null) {

                    lastVisible = documentSnapshots.documents[documentSnapshots.size() - 1]

                    for (doc in documentSnapshots) {
                        val note = doc.toObject<Note>(Note::class.java)
                            note.id = doc.id
                            notesList.add(note)
                            mAdapter?.notifyItemInserted(notesList.size)
                    }
                    mAdapter = NoteRecyclerViewAdapter(notesList, applicationContext, firestoreDB!!)
                }
                mAdapter = NoteRecyclerViewAdapter(notesList, applicationContext, firestoreDB!!)
                rvNoteList.adapter = mAdapter
            })
    }
}

If you have any other source on this issue please suggest me.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Jony Birbol
  • 41
  • 1
  • 5
  • Did you already put breakpoint in `loadMorePosts` and in the snapshot listener in there, and check the values of `lastVisible` and the `documentSnapshots` you get? – Frank van Puffelen Sep 13 '20 at 18:13
  • I think this **[answer](https://stackoverflow.com/questions/50741958/how-to-paginate-firestore-with-android)** might help. – Alex Mamo Sep 14 '20 at 08:45
  • @FrankvanPuffelen Would you please check my code on github: https://github.com/JonyBirbol/MyNotes – Jony Birbol Sep 14 '20 at 14:55

0 Answers0