Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class RecyclerPaginationListener(
- private val feedViewModel: FeedViewModel
- ) : RecyclerView.OnScrollListener() {
- private var scrollState = -1
- private val ITEMS_LEFT_IN_BOTTOM = 2
- override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
- super.onScrollStateChanged(recyclerView, newState)
- scrollState = newState
- if (!feedViewModel.isLoading && scrollState == RecyclerView.SCROLL_STATE_IDLE) {
- recyclerView.layoutManager?.let {
- val visibleItemCount = it.childCount
- val totalItemCount = it.itemCount
- val firstVisibleItemPosition = (it as LinearLayoutManager).findFirstVisibleItemPosition()
- if ((visibleItemCount + firstVisibleItemPosition) >= (totalItemCount - ITEMS_LEFT_IN_BOTTOM)
- && firstVisibleItemPosition >= 0
- && totalItemCount >= feedViewModel.feedSize
- ) {
- feedViewModel.loadMoreItems()
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement