I need to perform a task in a RecyclerView immediately before scrolling starts and immediately after it ends, but absolutely not when the scroll is still ongoing.

When the user scrolls by dragging the view up or down on the screen, it's easy. I just use the normal 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 ####
            }
        }
    }

The trouble is when users scroll the RecyclerView by grabbing the scrollbar at the right side of the view.

OnScroll fires but OnScrollStateChanged doesn't fire, so I have no way of knowing whether it's a new scroll (RecyclerView.SCROLL_STATE_DRAGGING), an ongoing scroll, or the end of a scroll (RecyclerView.SCROLL_STATE_IDLE)

How do you determine when a scrollbar-driven scroll is starting and ending? Is there a different listener for when the user interacts with the scrollbar, or something?

cheers

John

0 Answers