垂直ViewPager在Android 9上无法正常工作

时间:2019-01-23 09:01:03

标签: android android-viewpager

我使用此自定义viewpager进行垂直滚动。除了在Android 9上,它在所有Android版本上均能完美运行。滑动至底部页面无法正常工作。

您有一些调查方法吗?

非常感谢您!

public class VerticalViewPager extends ViewPager {

    public interface OnTouchListener {
        void onTouchDown();

        void onTouchUp();

        void onTouchMove();
    }

    private OnTouchListener mListener;

    public VerticalViewPager(Context context) {
        super(context);
        init();
    }

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

    @Override
    public void setOnTouchListener(View.OnTouchListener l) {
        super.setOnTouchListener(l);
    }

    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);
    }

    public void setOnTouchListener(OnTouchListener listener) {
        mListener = listener;
    }

    private class VerticalPageTransformer implements ViewPager.PageTransformer {

        @Override
        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 <= 1) { // [-1,1]
                view.setAlpha(1);

                // Counteract the default slide transition
                view.setTranslationX(view.getWidth() * -position);

                //set Y setNum to swipe in from top
                float yPosition = position * view.getHeight();
                view.setTranslationY(yPosition);

            } else { // (1,+Infinity]
                // This page is way off-screen to the right.
                view.setAlpha(0);
            }
        }
    }

    /**
     * Swaps the X and Y coordinates of your touch event.
     */
    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 touch coordinates to original reference frame for any child views
        return intercepted;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                mListener.onTouchDown();
                break;
            case MotionEvent.ACTION_UP:
                mListener.onTouchUp();
                break;
            case MotionEvent.ACTION_MOVE:
                mListener.onTouchMove();
                break;
        }

        return super.onTouchEvent(swapXY(ev));
    }
}

0 个答案:

没有答案