Search code examples
androidandroid-viewpagerontouch

Is there a way to check whether ViewPager is currently scrolling between pages?


I am combining ViewPager with onTouch for the same, as it should be possible to both scroll the ViewPager and click it. However, I do not want both triggered, so I am looking for a convenient way to instruct the OnTouch to ignore MotionEvents which triggered a page scroll. I can of course measure the distance between the ACTION_DOWN and ACTION_UP, but I am wondering whether there is an existing method to help me.


Solution

  • The solution I found allows me to identify whether there is a scroll in progress. scrollState has three values defined in ViewPager: SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING and SCROLL_STATE_SETTLING. Now I can check scrollState in the onTouch method and act accordingly.

    @Override
    public void onPageScrollStateChanged(int state) {
        scrollState=state;
    }