Advertisement
Guest User

RecyclerPaginationListener

a guest
Jul 15th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.06 KB | None | 0 0
  1. class RecyclerPaginationListener(
  2.     private val feedViewModel: FeedViewModel
  3. ) : RecyclerView.OnScrollListener() {
  4.  
  5.     private var scrollState = -1
  6.  
  7.     private val ITEMS_LEFT_IN_BOTTOM = 2
  8.  
  9.     override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
  10.         super.onScrollStateChanged(recyclerView, newState)
  11.         scrollState = newState
  12.         if (!feedViewModel.isLoading && scrollState == RecyclerView.SCROLL_STATE_IDLE) {
  13.             recyclerView.layoutManager?.let {
  14.                 val visibleItemCount = it.childCount
  15.                 val totalItemCount = it.itemCount
  16.                 val firstVisibleItemPosition = (it as LinearLayoutManager).findFirstVisibleItemPosition()
  17.                 if ((visibleItemCount + firstVisibleItemPosition) >= (totalItemCount - ITEMS_LEFT_IN_BOTTOM)
  18.                     && firstVisibleItemPosition >= 0
  19.                     && totalItemCount >= feedViewModel.feedSize
  20.                 ) {
  21.                     feedViewModel.loadMoreItems()
  22.                 }
  23.             }
  24.         }
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement