androidandroid-viewpager2

Detect overscroll or end of pages event in ViewPager2 android


there is no method for overscroll in ViewPager2 I need to detect the overscroll so I can take the user back to home page once there are no more pages.


Solution

  • ViewPager2 does not have a public method for over scroll, however, It can be detected by listening to onPageScrollStateChanged

    Usually transition of events in onPageScrollStateChanged for scroll state is SCROLL_STATE_DRAGGING -> SCROLL_STATE_SETTLING -> SCROLL_STATE_IDLE when changing a page

    But in case of overscroll the sequence is SCROLL_STATE_DRAGGING -> SCROLL_STATE_IDLE

    private fun listenOverScroll(currentIndex: Int, size: Int) {
            var index = currentIndex
            var previousState =  ViewPager2.SCROLL_STATE_IDLE
            viewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
    
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    index = position
                }
    
                override fun onPageScrollStateChanged(state: Int) {
                    Log.d(TAG,"Index:: $index | state:: $state | prevState:: $previousState")
                    super.onPageScrollStateChanged(state)
                    if ((index >= size - 1 || index <= 0)// end of list. these checks can be 
                                                         // used individualy to detect end or start of pages
                            && previousState == ViewPager2.SCROLL_STATE_DRAGGING // from DRAGGING
                            && state == ViewPager2.SCROLL_STATE_IDLE) {          // to IDLE
                        Log.d(TAG,"OVERSCROLL:: Index:: $index | state:: $state | prevState:: $previousState")
                        //overscroll performed. do your work here
                    }
                    previousState = state
                }
            })
        }