Open CTSN opened 4 years ago
你好,我用的平板横屏调试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; } /**
@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; } }
`
请问,如何控制滑动速度,无论慢滑还是快滑都只滑动一个item,而不是将数据集全部滑动一遍
你好,我用的平板横屏调试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; } /**
@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 smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) { // TODO 循环模式暂不支持平滑滚动
`