Bastien Léonard
Bastien Léonard

Reputation: 61713

Android view pager: how to distinguish user and programmatic swipes?

I use ViewPager.setCurrentItem() to automatically swipe to the next page every few seconds. I'd like to disable this as soon as the user starts swiping himself. As far as I can tell, OnPageChangedListener gets triggered in the same way whether the swipe came from the user or not. It seems like beginFakeDrag() could help, but it requires to drag by a specified number of pixels, which isn't practical.

Upvotes: 5

Views: 1484

Answers (1)

DroidBender
DroidBender

Reputation: 7892

Are you familiar with SCROLL_STATE_DRAGGING? It indicates that the pager is currently being dragged by the user.

Example

mPager.setOnPageChangeListener(new OnPageChangeListener() {


    @Override
    public void onPageSelected(int position) { 
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (state == ViewPager.SCROLL_STATE_DRAGGING) {
            // User has dragged
        }
    }
});

Upvotes: 15

Related Questions