Search code examples
androidandroid-viewpager

How to listen to when view pager crosses threshold to new page?


When swiping on a view pager. There comes a point where when you let go, it goes to the other page instead of the original one - AKA the mid point.

ViewPager.OnPageChangeListener.onPageSelected(int position) only gets called after the select.

I want to be notified the moment the user crosses the threshold. How should I go about doing this?


Solution

  • viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //This method will be invoked when the current page is scrolled, either as part of a
                // programmatically initiated smooth scroll or a user initiated touch scroll.
            }
    
            @Override
            public void onPageSelected(int position) {
                //This method will be invoked when a new page becomes selected.
            }
    
            @Override
            public void onPageScrollStateChanged(int state) {
                //Called when the scroll state changes.
            }
        });
    

    for more information please refer ViewPager.addOnPageChangeListener