Savage F. Morgan

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

The more general extension of ViewPager would bet to create a "SetPagingEnabled" method so that we can enable and disable paging on the fly. To enable / disable the swiping, just overide two methods: "onTouchEvent" and "onInterceptTouchEvent". Both will return "false" if the paging was disabled.

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

Then select this instead of the builtin viewpager in XML

<mypackage.CustomViewPager 
android:id="@+id/myViewPager" 
android:layout_height="match_parent" 
android:layout_width="match_parent" />

You just need to call the "setPagingEnabled" method with "false" and users won't be able to swipe to paginate

http://stackoverflow.com/questions/9650265/how-do-disable-paging-by-swiping-with-finger-in-viewpager-but-still-be-able-to-s

posted on 2014-04-19 19:01  罗斯摩根  阅读(694)  评论(0编辑  收藏  举报