Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: How to disable scrolling on the viewpager [duplicate]

In my android application, I have a viewpager which have a list of imageView. Each of the view can be drawn. When I draw on it, I can still swipe to the next view from viewpager. How can I disable the paging while user start to draw on a an imageView.

like image 537
LittleFunny Avatar asked Apr 02 '16 09:04

LittleFunny


People also ask

How do I stop ViewPager from scrolling?

A simple solution is to create your own subclass of ViewPager that has a private boolean flag, isPagingEnabled . Then override the onTouchEvent and onInterceptTouchEvent methods. If isPagingEnabled equals true invoke the super method, otherwise return .

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);

How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?

To enable / disable the swiping, just overide two methods: onTouchEvent and onInterceptTouchEvent . Both will return "false" if the paging was disabled. You just need to call the setPagingEnabled method with false and users won't be able to swipe to paginate.

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.


1 Answers

Add this as a class and use it in xml instead of your viewpager tag. Basically we make a customised viewpager, where we are disabling the swipeable behaviour by returning false to onInterceptTouchEvent and OnTouchEvent.

public class NonSwipeableViewPager extends ViewPager {

    public NonSwipeableViewPager(Context context) {
        super(context);
    }

    public NonSwipeableViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        // Never allow swiping to switch between pages
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Never allow swiping to switch between pages
        return false;
    }
}
like image 102
D Agrawal Avatar answered Sep 26 '22 17:09

D Agrawal