在一定的方向上的ViewPage停止刷卡在一、ViewPage

2023-09-12 22:57:35 作者:Smiledě味道

我怎么能阻止viewpager从滚动只在一个方向。例如,允许刷卡只有在正确的,而不是左侧。我绝对坚持,任何帮助将大大AP preciated。需要注意的是,我需要得到这个工作为Android 2.2版本,所以我现在用的相容性库ViewPager。

How can I stop viewpager from scrolling in one direction only. For example, allow swipes only to the right, but not to the left. I am absolutely stuck, any help would be greatly appreciated. To note that I need to get this to work for Android version 2.2, so I am using the compatibilty library for ViewPager.

在此先感谢

推荐答案

您将不得不作出自己的ViewPager,它扩展了原有的ViewPager和覆盖的onTouchEvent方法

You will have to make your own ViewPager that extends the original ViewPager and override the onTouchEvent method

public class CustomViewPager extends ViewPager {

    float lastX = 0;

    boolean lockScroll = false;

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

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

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            lastX = ev.getX();
            lockScroll = false;
            return super.onTouchEvent(ev);
        case MotionEvent.ACTION_MOVE:

            if (lastX > ev.getX()) {
                lockScroll = false;
            } else {
                lockScroll = true;
            }

            lastX = ev.getX();
            break;
        }

        lastX = ev.getX();

        if(lockScroll) {
            return false;
        } else {
            return super.onTouchEvent(ev);
        }

    }



}
 
精彩推荐
图片推荐