一个很尿性问题:重新刷新后 recyclerView.smoothScrollBy(-100, 0); 不起作用

一个很尿性问题:

首次加载可以,重新刷新后 recyclerView.smoothScrollBy(-100, 0); 是调用了,但不起作用,没有达预期效果!!!! 急,急,急。

首次加载可以,重新刷新后 recyclerView.smoothScrollBy(-100, 0); 是调用了,但不起作用,没有达预期效果!!!! 急,急,急。

首次加载可以,重新刷新后 recyclerView.smoothScrollBy(-100, 0); 是调用了,但不起作用,没有达预期效果!!!! 急,急,急。

为什么不用  滚动到指定位置 mRecyclerView.scrollToPosition(position); , 都是需求说了算,只需要移动一点点,一半之类的,无大语了。

scrollToPosition首次和刷新都正常,就是smoothScrollBy手动刷新后不起效果。

解决方案:

1.做个标记是否是手动刷新

2. 手动刷新时先执行 mRecyclerView.scrollToPosition(1);

       再进行recyclerView.smoothScrollBy(-100, 0);

居然可行,达到预期效果了。

各种GPT都没有结果,差点崩溃了,睡一觉起来,大胆试一试,居然成功了各位,香不香!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,我的回答还是有误。这是因为scrollBy是LinearLayoutManager的一个非公开的方法,只能在LinearLayoutManager内部使用,不能从外部访问。因此,我们需要通过其他方式来实现RecyclerView的平滑滚动。以下是一个基于Scroller类的实现方法: ``` public void smoothScrollBy(int dx, int dy) { RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(getContext()) { @Override public PointF computeScrollVectorForPosition(int targetPosition) { return LinearLayoutManager.this.computeScrollVectorForPosition(targetPosition); } }; smoothScroller.setTargetPosition(getPositionForVelocity(dx, dy)); startSmoothScroll(smoothScroller); } protected int getPositionForVelocity(int dx, int dy) { if (getChildCount() == 0) { return 0; } final View nearestChild = findNearestChild(); if (nearestChild == null) { return 0; } final int currentPosition = getPosition(nearestChild); if (currentPosition == RecyclerView.NO_POSITION) { return 0; } final int direction = computeScrollDirectionForPosition(currentPosition, dx, dy); if (direction == RecyclerView.NO_POSITION) { return 0; } final int targetPosition = currentPosition + direction; if (targetPosition < 0 || targetPosition >= getItemCount()) { return 0; } return targetPosition; } protected int computeScrollDirectionForPosition(int targetPosition, int dx, int dy) { if (getChildCount() == 0) { return RecyclerView.NO_POSITION; } final View firstChild = getChildAt(0); if (firstChild == null) { return RecyclerView.NO_POSITION; } final int currentPosition = getPosition(firstChild); if (currentPosition == RecyclerView.NO_POSITION) { return RecyclerView.NO_POSITION; } final int direction = getScrollDirectionForPosition(currentPosition, targetPosition); if (direction == RecyclerView.NO_POSITION) { return RecyclerView.NO_POSITION; } final int distance = computeDistanceToPosition(targetPosition); if (distance == 0) { return RecyclerView.NO_POSITION; } return (int) Math.ceil((double) distance / (double) getDistancePerChild()); } protected int getDistancePerChild() { return Math.round(getChildAt(0).getHeight() * 1.0f); } protected int computeDistanceToPosition(int targetPosition) { final int targetChildIndex = targetPosition - getPosition(getChildAt(0)); return targetChildIndex * getDistancePerChild(); } protected int getScrollDirectionForPosition(int currentPosition, int targetPosition) { return targetPosition < currentPosition ? -1 : 1; } protected View findNearestChild() { final int childCount = getChildCount(); if (childCount == 0) { return null; } View nearestChild = null; int nearestDistance = Integer.MAX_VALUE; final int centerX = getWidth() / 2; final int centerY = getHeight() / 2; for (int i = 0; i < childCount; i++) { final View child = getChildAt(i); final int distanceX = Math.abs(getLeftDecorationWidth(child) + getRightDecorationWidth(child) + child.getMeasuredWidth() / 2 - centerX); final int distanceY = Math.abs(getTopDecorationHeight(child) + getBottomDecorationHeight(child) + child.getMeasuredHeight() / 2 - centerY); final int distance = distanceX * distanceX + distanceY * distanceY; if (distance < nearestDistance) { nearestChild = child; nearestDistance = distance; } } return nearestChild; } ``` 这个实现方法是通过LinearSmoothScroller来实现的,首先创建一个LinearSmoothScroller对象,然后设置目标位置,最后启动平滑滚动。在getPositionForVelocity、computeScrollDirectionForPosition、computeDistanceToPosition和findNearestChild等函数中,实现了计算滚动距离、滚动方向、目标位置和最近的子View等功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值