[Kotlin] RecyclerView Scrolling list

2 views
Skip to first unread message

seo seo

unread,
Jul 29, 2022, 3:07:57 AM7/29/22
to kamjaroid
addOnScrollListener(th...@BreakingNewsFragment.scrollListener)

private val scrollListener = object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
if(newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL){ //State is scrolling
isScrolling = true
}
}

override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)

val layoutManager = recyclerView.layoutManager as LinearLayoutManager
val firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition()
val totalVisibleItemCount = layoutManager.childCount
val totalItemCount = layoutManager.itemCount

val isNotLoadingAndNotLastPage = !isLoading && !isLastPage
val isAtLastItem = firstVisibleItemPosition + totalVisibleItemCount >= totalItemCount
val isNotAtBeginning = firstVisibleItemPosition >= 0
val isTotalMoreThanVisible = totalItemCount >= QUERY_PAGE_SIZE
val shouldPaginate = isNotLoadingAndNotLastPage && isAtLastItem && isNotAtBeginning && isTotalMoreThanVisible && isScrolling

if(shouldPaginate){
viewModel.getBreakingNews("tr")
isScrolling = false
}
}
}
Reply all
Reply to author
Forward
0 new messages