Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

调用smoothScrollPosition滚动指定位置,会出现回滚到原来位置 #19

Open
CTSN opened this issue Jul 17, 2020 · 1 comment

Comments

@CTSN
Copy link

CTSN commented Jul 17, 2020

你好,我用的平板横屏调试demo,出现这个问题。
我debug看了下,
位置0->位置1,正常调用smoothScrollToPosition
位置1->位置2,调用smoothScrollToPosition同时会调用scrollHorizontallyBy,导致mOffsetAll重新计算,导致滚动失败。
我尝试着在scrollHorizontallyBy 和 fixOffsetWhenFinishScroll方法里加个判断,return出去不触发,恢复正常使用。具体原因分析不出来。
我的修改:
` @OverRide
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler,
RecyclerView.State state) {
if (isSmoothScroll){
Log.e("isSmoothScroll" , "return");
return 0;
}
if (mAnimation != null && mAnimation.isRunning()) mAnimation.cancel();
int travel = dx;
if (!mIsLoop) { //非循环模式,限制滚动位置
if (dx + mOffsetAll < 0) {
travel = -mOffsetAll;
} else if (dx + mOffsetAll > getMaxOffset()){
travel = (int) (getMaxOffset() - mOffsetAll);
}
}
mOffsetAll += travel; //累计偏移量
layoutItems(recycler, state, dx > 0 ? SCROLL_TO_LEFT : SCROLL_TO_RIGHT);
return travel;
}
/**
* 修正停止滚动后,Item滚动到中间位置
/
private void fixOffsetWhenFinishScroll() {
if (isSmoothScroll){
Log.e("smoothScroll","return fixOffsetWhenFinishScroll");
return;
}
int scrollN = (int) (mOffsetAll * 1.0f / getIntervalDistance());
float moreDx = (mOffsetAll % getIntervalDistance());
double judgeDistance = getIntervalDistance() * 0.5;
if (Math.abs(moreDx) > judgeDistance) {
if (moreDx > 0) scrollN ++;
else scrollN --;
}
int finalOffset = (int) (scrollN * getIntervalDistance());
startScroll(mOffsetAll, finalOffset);
mSelectPosition = Math.abs(Math.round (finalOffset * 1.0f / getIntervalDistance())) % getItemCount();
}
/
*
* 滚动到指定X轴位置
* @param from X轴方向起始点的偏移量
* @param to X轴方向终点的偏移量
*/
private isSmoothScroll = false;
private void startScroll(int from, int to) {
if (mAnimation != null && mAnimation.isRunning()) {
mAnimation.cancel();
}
final int direction = from < to ? SCROLL_TO_LEFT : SCROLL_TO_RIGHT;
mAnimation = ValueAnimator.ofFloat(from, to);
mAnimation.setDuration(500);
mAnimation.setInterpolator(new DecelerateInterpolator());
mAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@OverRide
public void onAnimationUpdate(ValueAnimator animation) {
mOffsetAll = Math.round((float) animation.getAnimatedValue());
layoutItems(mRecycle, mState, direction);
}
});
mAnimation.addListener(new Animator.AnimatorListener() {
@OverRide
public void onAnimationStart(Animator animation) {
}

        @Override
        public void onAnimationEnd(Animator animation) {
            onSelectedCallBack();
            isSmoothScroll = false;
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });
    mAnimation.start();
}

@OverRide
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
// TODO 循环模式暂不支持平滑滚动

    if (mIsLoop) return;
    int finalOffset = calculateOffsetForPosition(position);
    if (mRecycle == null || mState == null) {//如果RecyclerView还没初始化完,先记录下要滚动的位置
        mSelectPosition = position;
    } else {
        startScroll(mOffsetAll, finalOffset);
        isSmoothScroll = true;
    }
}

`

@lxb89
Copy link

lxb89 commented Oct 26, 2020

请问,如何控制滑动速度,无论慢滑还是快滑都只滑动一个item,而不是将数据集全部滑动一遍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants