如何像 ViewPager 一样创建平滑的滑动?

How to create smooth swipe as in ViewPager?

我需要在应用程序中向回滑动编码。但是,使用 ViewPager 不适合设计,因此我必须编写滑动逻辑代码。

我过去通过 Gestures 做过几十次,但是滑动从来没有像 ViewPager 那样流畅。这不可靠。有时它工作正常,但经常无法识别滑动,所以我不得不再次滑动。另一个问题是滑动操作会调用 "pull to refresh" 指示器,因此不会再次激活滑动。

有没有刷卡逻辑完美的库?如果没有,我会 post 我通常使用的滑动逻辑,所以你可以告诉我它的缺陷。

这是我的onSwipeTouchListenerclass。

public class OnSwipeTouchListener implements View.OnTouchListener {

private GestureDetector gestureDetector;

public OnSwipeTouchListener(Context c) {
    gestureDetector = new GestureDetector(c, new GestureListener());
}

public boolean onTouch(final View view, final MotionEvent motionEvent) {
    return gestureDetector.onTouchEvent(motionEvent);
}

private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 10;
    private static final int SWIPE_VELOCITY_THRESHOLD = 10;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        onClick();
        return super.onSingleTapUp(e);
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        onDoubleClick();
        return super.onDoubleTap(e);
    }

    @Override
    public void onLongPress(MotionEvent e) {
        onLongClick();
        super.onLongPress(e);
    }

    // Determines the fling velocity and then fires the appropriate swipe event accordingly
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeDown();
                    } else {
                        onSwipeUp();
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeUp() {
}

public void onSwipeDown() {
}

public void onClick() {

}

public void onDoubleClick() {

}

public void onLongClick() {

}

}

这是您的使用方法。

mainView.setOnTouchListener(new OnSwipeTouchListener(getActivity()) {

        @Override
        public void onSwipeUp() {
               // Do Stuff here
        }

        @Override
        public void onClick() {
                // Do Stuff here

        }
    });

很顺利。我尝试了许多其他示例,但这是最好的一个。 您还可以设置滑动手势的阈值。您需要做的就是更改这些变量。

private static final int SWIPE_THRESHOLD = 10;
private static final int SWIPE_VELOCITY_THRESHOLD = 10;

它可以检测LeftSwipeRightSwipeUpSwipeDownSwipeOnClickOnDoubleClickOnLongClick

我用这个 class 来改变 UpSwipeDownSwipe 手势的片段。

使用 PageStrip。 很顺滑很好用

http://codetheory.in/android-pagertabstrip-pagertitlestrip-viewpager/