我需要在滚动开始之前和结束之后立即在RecyclerView中执行任务,但是绝对不需要在滚动仍在进行时。
当用户通过在屏幕上上下拖动视图来滚动时,这很容易。我只使用普通的RecyclerView.OnScrollListener
:
private val mInfinityScrollListener = object : RecyclerView.OnScrollListener() {
private var pageHasScrolled: Boolean = false
private var scrollIsStarting: Boolean = false
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
scrollIsStarting = true
}
if (newState == RecyclerView.SCROLL_STATE_IDLE && pageHasScrolled) {
pageHasScrolled = false
####PERFORM END TASKS HERE####
}
}
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
pageHasScrolled = true
if (scrollIsStarting) {
scrollIsStarting = false
### PERFORM INITIAL TASKS HERE ####
}
}
}
麻烦在于,当用户通过抓住视图右侧的滚动条滚动RecyclerView时。
OnScroll
被触发,但是OnScrollStateChanged
不被触发,因此我无法知道它是新滚动(RecyclerView.SCROLL_STATE_DRAGGING
),正在进行的滚动还是滚动的结束(RecyclerView.SCROLL_STATE_IDLE
)
如何确定滚动条驱动的滚动何时开始和结束?当用户与滚动条进行交互时,是否有其他监听器?
欢呼
约翰