Created
October 18, 2015 08:03
-
-
Save Bodo1981/a3e6dc7c9505b49ff2e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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