Направленный ViewPager испытывает проблему с контролем прокрутки - PullRequest
0 голосов
/ 04 июня 2018

Я использую пользовательский направленный ViewPager.У меня есть поток, похожий на Viewpager внутри Viewpager, поэтому я должен отключить или включить родительский ViewPager, чтобы заставить работать дочерние пейджеры.Проблема, с которой я сталкиваюсь, заключается в том, что, если я устанавливаю направление на одну сторону, то есть влево / вправо, то на Scroll он все еще движется в этом направлении небольшими рывками, то есть 10,20 dp с каждым пролистыванием.Ниже приведен код, кажется, проблема с перехватом в неправильном состоянии.

public class DirectionalViewPager extends ViewPager {
    private static float DIRECTION_THRESHOLD; //The slop
    float diffX, diffY;
    private float initialXValue;
    private float initialYValue;
    private SwipeDirection direction;
    private ScrollerCustomDuration mScroller = null;

    public DirectionalViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.direction = SwipeDirection.HORIZONTAL;
        DIRECTION_THRESHOLD = context.getResources().getDimension(R.dimen.small_spacing);
        postInitViewPager();
    }

    /**
     * Override the Scroller instance with our own class so we can change the
     * duration
     */
    private void postInitViewPager() {
        try {
            Field scroller = ViewPager.class.getDeclaredField("mScroller");
            scroller.setAccessible(true);
            Field interpolator = ViewPager.class.getDeclaredField("sInterpolator");
            interpolator.setAccessible(true);

            mScroller = new ScrollerCustomDuration(getContext(),
                    (Interpolator) interpolator.get(null));
            scroller.set(this, mScroller);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Set the factor by which the duration will change
     */
    public void setScrollDurationFactor(double scrollFactor) {
        mScroller.setScrollDurationFactor(scrollFactor);

    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        if (isSwipeAllowed(ev) && ev.getActionMasked() != MotionEvent.ACTION_DOWN
                && ev.getActionMasked() != MotionEvent.ACTION_UP)
            return true;
        else
            return super.onInterceptTouchEvent(ev);
        //return isSwipeAllowed(ev) && super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        return isSwipeAllowed(ev) && super.onTouchEvent(ev);

    }

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

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

        int action = event.getActionMasked();

        if (action == MotionEvent.ACTION_DOWN) {
            initialXValue = event.getX();
            initialYValue = event.getY();
            return true;
        } else if (action == MotionEvent.ACTION_MOVE) {
            try {
                diffX = event.getX() - initialXValue;
                diffY = event.getY() - initialYValue;
                boolean isVertical = true;
                boolean isAboveThreshold = Math.abs(diffX) > DIRECTION_THRESHOLD;

                if (Math.abs(diffY) >= DIRECTION_THRESHOLD || Math.abs(diffX) >= DIRECTION_THRESHOLD)
                    isVertical = Math.abs(diffY) >= Math.abs(diffX);

                    if (diffX < 0 && direction == SwipeDirection.RIGHT) {
                    // left intercept is detected
                    return false;
                } else if (diffX > 0 && direction == SwipeDirection.LEFT) {
                    // right intercept is detected
                    return false;
                } else if (isVertical && direction == SwipeDirection.HORIZONTAL) {
                    // vertical swipe is detected.  Lets intercept by returning true
                    return false;
                }

            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }

        return true;
    }

    public SwipeDirection getSwipeDirection() {
        return direction;
    }

    public void setSwipeDirection(SwipeDirection direction) {
        this.direction = direction;
    }

    /**
     * Select the direction that you want to enable, others will be ignored
     */
    public enum SwipeDirection {

        ALL(0),
        NONE(1),
        LEFT(2),
        RIGHT(3),
        UP(4),
        DOWN(5),
        VERTICAL(6),
        HORIZONTAL(7),;
        private final int mType;

        SwipeDirection(int type) {
            mType = type;
        }

        public int getType() {
            return mType;
        }

    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...