androidx / constraintlayout

ConstraintLayout is an Android layout component which allows you to position and size widgets in a flexible way
Apache License 2.0
1.06k stars 177 forks source link

Carousel backward problem #429

Open cla99ic opened 2 years ago

cla99ic commented 2 years ago

On carousel demo, on right swiping, it goes a little back then it continues by going forward.

Homailot commented 3 weeks ago

Is there any update regarding this issue?

Homailot commented 3 weeks ago

It seems to be related to this runnable in the Carousel class

Runnable mUpdateRunnable = new Runnable() {
    @Override
    public void run() {
        mMotionLayout.setProgress(0);
        updateItems();
        mAdapter.onNewItem(mIndex);
        float velocity = mMotionLayout.getVelocity();
        if (mTouchUpMode == TOUCH_UP_CARRY_ON && velocity > mVelocityThreshold
                && mIndex < mAdapter.count() - 1) {
            final float v = velocity * mDampening;
            if (mIndex == 0 && mPreviousIndex > mIndex) {
                // don't touch animate when reaching the first item
                return;
            }
            if (mIndex == mAdapter.count() - 1 && mPreviousIndex < mIndex) {
                // don't touch animate when reaching the last item
                return;
            }
            mMotionLayout.post(new Runnable() {
                @Override
                public void run() {
                    mMotionLayout.touchAnimateTo(MotionLayout.TOUCH_UP_DECELERATE_AND_COMPLETE,
                            1, v);
                }
            });
        }
    }
};

Specifically this call to mMotionLayout.touchAnimateTo(MotionLayout.TOUCH_UP_DECELERATE_AND_COMPLETE, 1, v);. Since velocity seems to be always positive even when swiping right or left, this piece of code will always move to the next view of the carousel. I've tried swapping the position value of the call from 1 to -1, but the result remains the same