Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Helper listener class to implement pagination
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
@Suppress("unused")
abstract class EndlessRecyclerOnScrollListener(
manager: LinearLayoutManager
) : RecyclerView.OnScrollListener() {
private var layoutManager: LinearLayoutManager = manager
abstract fun onLoadMore(lastVisibleIndex: Int)
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val firstVisibleItemIndex: Int = layoutManager.findFirstVisibleItemPosition()
val visibleItemCount: Int = layoutManager.childCount
val totalItemCount: Int = layoutManager.itemCount
val delta = if (layoutManager.orientation == LinearLayoutManager.HORIZONTAL) dx else dy
val startToEnd: Boolean = !layoutManager.reverseLayout
if ((if (startToEnd) delta > 0 else delta < 0) && firstVisibleItemIndex + visibleItemCount >= totalItemCount) {
onLoadMore(layoutManager.findLastVisibleItemPosition())
}
}
}
@GuilhE

This comment has been minimized.

Copy link
Owner Author

@GuilhE GuilhE commented Aug 31, 2019

Same use as in here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment