Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
EndlessRecyclerViewScrollListener file for kotlin projects
/**
* Created by Luca Nicoletti
* © 17/10/2018
* Copied from: https://gist.github.com/nesquena/d09dc68ff07e845cc622; added some little edits for Kotlin
*/
abstract class EndlessRecyclerViewScrollListener(private var visibleThreshold: Int = 5): RecyclerView.OnScrollListener() {
private var currentPage = 0
private var previousTotalItemCount = 0
private var loading = true
private val startingPageIndex = 0
private var mLayoutManager: RecyclerView.LayoutManager? = null
constructor(visibleThreshold: Int, layoutManager: LinearLayoutManager): this(visibleThreshold) {
this.mLayoutManager = layoutManager
}
constructor(visibleThreshold: Int, layoutManager: GridLayoutManager): this(visibleThreshold) {
this.mLayoutManager = layoutManager
this.visibleThreshold = visibleThreshold * layoutManager.spanCount
}
constructor(visibleThreshold: Int, layoutManager: StaggeredGridLayoutManager): this(visibleThreshold) {
this.mLayoutManager = layoutManager
this.visibleThreshold = visibleThreshold * layoutManager.spanCount
}
fun getLastVisibleItem(lastVisibleItemPositions: IntArray): Int {
var maxSize = 0
for (i in lastVisibleItemPositions.indices) {
if (i == 0) {
maxSize = lastVisibleItemPositions[i]
} else if (lastVisibleItemPositions[i] > maxSize) {
maxSize = lastVisibleItemPositions[i]
}
}
return maxSize
}
override fun onScrolled(view: RecyclerView, dx: Int, dy: Int) {
var lastVisibleItemPosition = 0
val totalItemCount = mLayoutManager?.itemCount ?: 0
if (mLayoutManager is StaggeredGridLayoutManager) {
val lastVisibleItemPositions = (mLayoutManager as StaggeredGridLayoutManager).findLastVisibleItemPositions(null)
lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions)
} else if (mLayoutManager is GridLayoutManager) {
lastVisibleItemPosition = (mLayoutManager as GridLayoutManager).findLastVisibleItemPosition()
} else if (mLayoutManager is LinearLayoutManager) {
lastVisibleItemPosition = (mLayoutManager as LinearLayoutManager).findLastVisibleItemPosition()
}
if (totalItemCount < previousTotalItemCount) {
this.currentPage = this.startingPageIndex
this.previousTotalItemCount = totalItemCount
if (totalItemCount == 0) {
this.loading = true
}
}
if (loading && totalItemCount > previousTotalItemCount) {
loading = false
previousTotalItemCount = totalItemCount
}
if (!loading && lastVisibleItemPosition + visibleThreshold > totalItemCount) {
currentPage++
onLoadMore(currentPage, totalItemCount, view)
loading = true
}
}
fun resetState() {
this.currentPage = this.startingPageIndex
this.previousTotalItemCount = 0
this.loading = true
}
abstract fun onLoadMore(page: Int, totalItemsCount: Int, view: RecyclerView)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.