禁止在ViewPager上重新滑动

时间:2016-09-12 14:17:37

标签: android android-viewpager

我有一个扩展ViewPager的类,它包含以下方法:

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

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

/**
 * Only allow forward flow in tutorial screens. This method returns false if user tries to
 * drag from left to right, i.e. trying to get to a previous string
 */
private boolean isSwipeAllowed(MotionEvent event) {
    if(event.getAction()==MotionEvent.ACTION_DOWN) {
        initialXValue = event.getX();
        return true;
    }

    if(event.getAction()==MotionEvent.ACTION_MOVE) {
        try {
            float diffX = event.getX() - initialXValue;
            if (diffX > 0 ) {
                // swipe from left to right detected
                return false;
            }else if (diffX < 0 ) {
                // swipe from right to left detected
                return true;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    return true;
} 

这可以防止用户从左到右拖动回到上一个片段。但是,如果用户从右向左轻微拖动,然后在同一滑动中快速从左向右拖动,则前一个片段再次成为焦点。

我该如何防止这种情况?

-Otterman

0 个答案:

没有答案