23

我想禁用滑动,但只在右侧。我在这个答案中找到了一个可行的解决方案。不幸的是,这复制了整个ViewPager源代码以实现目标。是否有任何方法只是继承现有类而不复制?

4

7 回答 7

25

我不确定这是否正是您所需要的:我需要一个用于向导的 viewpager,该向导具有用户无法通过的最大页面。

最后,解决方案在适配器中。我更改了 PagerAdapter 的计数,这样可以阻止用户传递最大页面:

@Override
public int getCount() {
    return mProgress; //max page + 1
}

当用户进入下一页时:

private void setWizardProgress(int progress) {
    if(progress > mProgress) {
        mProgress = progress;
        mWizardPagerAdapter.notifyDataSetChanged();
    }
}

这样,当用户处于最大页面时,他无法向右滚动。

于 2014-02-06T12:46:13.870 回答
23

这是工作 ViewPager 类,可以禁用任何方向分页。在这里查看所有答案

public class CustomViewPager extends ViewPager {
    private float initialXValue;
    private SwipeDirection direction;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.direction = SwipeDirection.all;
    }

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

        return false;
    }

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

        return false;
    }

    private boolean IsSwipeAllowed(MotionEvent event) {
        if(this.direction == SwipeDirection.all) return true;

        if(direction == SwipeDirection.none )//disable any swipe
            return false;

        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 && direction == SwipeDirection.right ) {
                    // swipe from left to right detected
                    return false;
                }else if (diffX < 0 && direction == SwipeDirection.left ) {
                    // swipe from right to left detected
                    return false;
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }

        return true;
    }

    public void setAllowedSwipeDirection(SwipeDirection direction) {
        this.direction = direction;
    }
于 2015-12-03T21:43:44.107 回答
10

另一种简单的方法是使用 setCurrentItem() 如果您点击某个位置,则可以滚动回所需的幻灯片。例如,这将只允许向前滑动:

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

    @Override
    public void onPageSelected(int position) {
        if(position < mProgress) {
            mViewPager.setCurrentItem(mProgress, true);
        } else {
            mProgress = position;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {}
});

或者,如果您想要最大幻灯片:

if(position > 4) {
    mViewPager.setCurrentItem(4, true);
}

此解决方案在技术上不会完全禁用滑动,因为当您进行滑动移动时,您仍会看到一小部分不允许的滑动。但对于某些应用程序,这可能是首选。

于 2016-08-18T13:27:08.567 回答
4

您可以尝试以下操作:

第 1 步:创建一个新的自定义类,比如“ CustomViewPager”。该类继承自 " ViewPager" 并包含一个名为 " setPagingEnabled" 的新自定义方法,目的是根据要求启用/禁用滑动。

Step2:覆盖两个方法:“ onTouchEvent”和“ onInterceptTouchEvent”。false如果要完全禁用分页,两者都将返回“ ”。

第三步:用自定义类替换ViewPager布局文件上的“”标签:

    <package_name.customviewpager 
     android:id="@+id/customViewPager" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" />

第 4 步: CustomViewPager.java

    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 && detectSwipeToRight(event)) {
        return super.onTouchEvent(event);
    }

    return false;
}

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

    return false;
}

// To enable/disable swipe 
public void setPagingEnabled(boolean enabled) {
    this.enabled = enabled;
}

// Detects the direction of swipe. Right or left. 
// Returns true if swipe is in right direction
public boolean detectSwipeToRight(MotionEvent event){

 int initialXValue = 0; // as we have to detect swipe to right
 final int SWIPE_THRESHOLD = 100; // detect swipe
 boolean result = false;

        try {                
            float diffX = event.getX() - initialXValue;

                if (Math.abs(diffX) > SWIPE_THRESHOLD ) {
                    if (diffX > 0) {
                        // swipe from left to right detected ie.SwipeRight
                        result = false;
                    } else {
                        // swipe from right to left detected ie.SwipeLeft
                        result = true;
                    }
                }
            } 
         catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}
于 2013-09-06T15:14:35.010 回答
3

您可以使用方法beginFakeDrag()endFakeDrag()

beginFakeDrag()当您想要禁用滑动并且endFakeDrag()想要再次启用时。

像这样:viewPager.beginFakeDrag();

于 2014-08-21T09:23:14.467 回答
1
private float initialXValue;
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (this.mEnabled) {
        return super.onTouchEvent(event);
    }
    if(event.getAction()==MotionEvent.ACTION_DOWN){
        initialXValue = event.getX();
    }else if(event.getAction()==MotionEvent.ACTION_MOVE){
        if(detectSwipeToRight(event)){
            System.out.println("right swipe detected");
        }
    }
    return true;
}

private boolean detectSwipeToRight(MotionEvent event) {
    final int SWIPE_THRESHOLD = 100; // detect swipe
    boolean result = false;

    try {
        float diffX = event.getX() - initialXValue;
        if (Math.abs(diffX) > SWIPE_THRESHOLD) {
            if (diffX < 0) {
                // swipe from right to left detected ie.SwipeLeft
                result = true;
            }
        }
    } catch (Exception exception) {
        exception.printStackTrace();
    }
    return result;
}
于 2014-06-21T10:48:28.910 回答
-3

您必须创建自己的 ViewPager 子类并覆盖 canScrollHorizo​​ntally 函数

http://developer.android.com/reference/android/support/v4/view/ViewPager.html#canScrollHorizo​​ntally(int)

于 2013-09-06T14:31:42.060 回答