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.