Wook No.1
RecyclerView 무한 스크롤 본문
RecyclerView addScrollListener 등록
abstract class EndlessRecyclerViewScrollListener(val layoutManager: LinearLayoutManager, private val startingPageIndex: Int = 0) : RecyclerView.OnScrollListener() {
private val visibleThreshold = 5
private var currentPage = 0
private var previousTotalItemCount = 0
private var loading = true
init {
currentPage = startingPageIndex
}
override fun onScrolled(view: RecyclerView, dx: Int, dy: Int) {
val lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition()
val totalItemCount = layoutManager.itemCount
if (totalItemCount == 0) {
loading = true
}
if (loading && totalItemCount > previousTotalItemCount) {
loading = false
previousTotalItemCount = totalItemCount
}
if (!loading && lastVisibleItemPosition + visibleThreshold > totalItemCount) {
currentPage++
onLoadMore(currentPage, totalItemCount, view)
loading = true
}
}
fun resetState() {
currentPage = startingPageIndex
previousTotalItemCount = 0
loading = true
}
abstract fun onLoadMore(page: Int, totalItemsCount: Int, view: RecyclerView?)
}
'Android' 카테고리의 다른 글
[빌드에러] Could not create service of type FileAccessTimeJournal using GradleUserHomeScopeServices (0) | 2022.05.27 |
---|---|
안드로이드 스튜디오 Gradle View 에서 Task 목록이 안보일때 (0) | 2022.05.24 |
CameraX 카메라앱 개발하기 (0) | 2022.04.27 |
구글 SafetyNet reCAPTCHA API(Android) 사용 (0) | 2022.04.19 |
Gradle JDK 변경 (0) | 2022.04.04 |
Comments