ViewPager取消左右滑动效果

最近做的项目中要求ViewPage禁止左右滑动,网上找了一下资料,自定议一个ViewPager就可以了,下面是代码
public class CustomViewPager extends ViewPager {

    private boolean enabled;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.enabled = true;
    }

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

        return false;
    }

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

        return false;
    }

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


在xml文件中用我们刚刚写的viewPager
<com.rosevision.ofashion.view.CustomViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/divider" />


再到代码里设置viewPager就可以了
CustomViewPager pager = (CustomViewPager) findViewById(R.id.pager);
        pager.setPagingEnabled(false);



你可能感兴趣的:(android,viewpager)