2017年8月25日 星期五

How to disable scrolling on ViewPager

Reference:

https://stackoverflow.com/questions/7814017/is-it-possible-to-disable-scrolling-on-a-viewpager

Code snippet:

1. Create a new CustomViewPager class
public class CustomViewPager extends ViewPager {

    private boolean isPagingEnabled = true;

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

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

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onInterceptTouchEvent(event);
    }

    public void setPagingEnabled(boolean b) {
        this.isPagingEnabled = b;
    }
}

2. Replace the <com.android.support.V4.ViewPager> tag with <your_package_name.CustomViewPager > tag in layout.xml

3. Call setPagingEnabled(true) when you want to disable Scrolling and call setPagingEnabled(false) to enable it.

沒有留言: