2

我正在创建一个应用程序,所以我使用了垂直视图分页器,视图分页器工作正常,但滚动不顺畅,有时在滑动时会卡住。这是我的代码,任何人都可以帮助我。提前谢谢。

 public class VerticalViewPager extends ViewPager {
public VerticalViewPager(Context context) {
    super(context);
    init();
}

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

private void init() {
    // The majority of the magic happens here
    setPageTransformer(true, new VerticalPageTransformer());
    // The easiest way to get rid of the overscroll drawing that happens on the left and right
    setOverScrollMode(OVER_SCROLL_NEVER);
}


private static class VerticalPageTransformer implements PageTransformer {
    private static float MIN_SCALE = 0.95f;

    public void transformPage(View view, float position) {


        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
            view.setAlpha(0);


        } else if (position <= 0) { // [-1,0]

            view.setAlpha(1);
            //view.setTranslationX(1);
            view.setScaleX(1);
            view.setScaleY(1);
            float yPosition = position * view.getHeight();
            view.setTranslationY(yPosition);
            view.setTranslationX(-1 * view.getWidth() * position);

        } else if (position <= 1) { // (0,1]
            // Fade the page out.
            view.setAlpha(1 - position);

            view.setTranslationX(-1 * view.getWidth() * position);

            // Scale the page down (between MIN_SCALE and 1)
            float scaleFactor = MIN_SCALE
                    + (1 - MIN_SCALE) * (1 - Math.abs(position));
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);

        } else {
            view.setAlpha(0);
        }

    }
}

private MotionEvent swapXY(MotionEvent ev) {
    float width = getWidth();
    float height = getHeight();

    float newX = (ev.getY() / height) * width;
    float newY = (ev.getX() / width) * height;

    ev.setLocation(newX, newY);

    return ev;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
    swapXY(ev);
    return intercepted;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    return super.onTouchEvent(swapXY(ev));
}
4

2 回答 2

0

是的,您需要做的就是将此行添加到视图寻呼机类:

@Override
public boolean canScrollHorizontally(int direction) {
    return false;
}
于 2016-03-22T07:31:13.037 回答
0

我对垂直有同样的问题viewpager。最后,我通过添加这个属性得到了解决方案:

android:clickable="false"

viewpager适配器的项目视图中。因此,将其添加到没有任何功能的视图中(即imageview取决于的布局textview) 。onClick

于 2016-12-03T11:28:15.807 回答