Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 861
Bastien Léonard Avatar asked Aug 17 '12 11:08

Bastien Léonard


People also ask

How do I know if my ViewPager is scrolling left or right android?

You save the current page of the ViewPager in OnPageSelected() and compare it to the position parameter of OnPageScrolled() . If the current page is less than or equal to the position , you are scrolling to the right, if not, you are scrolling to the left.

What is difference between ViewPager and ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .

What is the use of ViewPager in Android?

ViewPager in Android allows the user to flip left and right through pages of data. In our android ViewPager application we'll implement a ViewPager that swipes through three views with different images and texts.

How do I stop ViewPager from sliding on Android?

-Finally, you can disable it: view_pager. disableScroll(true); or enable it: view_pager. disableScroll(false);


1 Answers

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
        }
    }
});
like image 190
DroidBender Avatar answered Sep 27 '22 18:09

DroidBender