Skip to content

Instantly share code, notes, and snippets.

@Bodo1981
Created October 18, 2015 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bodo1981/a3e6dc7c9505b49ff2e9 to your computer and use it in GitHub Desktop.
Save Bodo1981/a3e6dc7c9505b49ff2e9 to your computer and use it in GitHub Desktop.
class LoadMoreScrollListener(val layoutManager: LinearLayoutManager, val onLoadMore: () -> Unit) : RecyclerView.OnScrollListener() {
companion object {
private val ITEMS_BEFORE_LAST_ITEM = 5
private val TAG = LoadMoreScrollListener::class.javaClass.canonicalName
}
private var isLoading: Boolean = false
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val lastVisibleItem = layoutManager.findLastVisibleItemPosition() + 1 // +1 because position starts at 0
val totalItems = layoutManager.itemCount
if (!isLoading) {
if (lastVisibleItem + ITEMS_BEFORE_LAST_ITEM >= totalItems) {
onLoadMore()
}
}
Log.d(TAG, "$totalItems -> $lastVisibleItem")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment