Here's my function that detect end off RecyclerView
that will be use in pagination later.
private fun setOnScrollListener(categoryRecyclerView: RecyclerView, categoryPresenter: EntertainmentPresenter){
categoryRecyclerView.addOnScrollListener(object: RecyclerView.OnScrollListener(){
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val totalItemCount:Int = categoryLayoutManager.itemCount
val firstVisibleItemPosition:Int = categoryLayoutManager.findFirstVisibleItemPosition()
val visibleItemCount:Int
val lastVisibleItemPosition: Int = categoryLayoutManager.findLastVisibleItemPosition()
visibleItemCount = lastVisibleItemPosition - firstVisibleItemPosition
Log.e("q", lastVisibleItemPosition.toString())
Log.e("q", dy.toString())
if (loading) {
if (totalItemCount > previousTotal) {
loading = false
previousTotal = totalItemCount
}
}
if (!loading && totalItemCount - visibleItemCount <= firstVisibleItemPosition) {
Log.e("q", lastVisibleItemPosition.toString())
loading = true
}
}
})
}
I used it before on RecyclerView
that has linear LayoutManger
and every thing was great.
But when I used it with RecyclerView
that has GridLayoutManager
, things got weird.
- It implemented only once and doesn't work when i scroll.
- Last visible item position return
totalItemCount-1
.
Last thing: this I call this function in onCreate
and I tried it with RecyclerView
that has linear layout manager
and everything was great
Here's my logcat,
2018-10-06 06:10:15.919 11033-11033/com.example.karem.moviesdatabase E/q: 0
2018-10-06 06:10:15.919 11033-11033/com.example.karem.moviesdatabase E/q: 18
It only called once and nothing happen when I scroll.
my RecyclerView height was set to match_parent
but after i change height to be 500dp everything works well
i don't know why but anyway here's my old layout
i only change recyclerview height to 500dp and it works
answer more weird than the question