Recyclerview smoothScrollToPosition()无效问题

自定义布局管理器实现平滑滚动
文章展示了如何在Android中对`SmoothCalendarLayoutManager`和`GridLayoutManager`进行重写,以实现更平滑的滚动效果。通过对`LinearSmoothScroller`的`calculateSpeedPerPixel`方法调整,控制滑动速度,使滚动看起来更自然。

对layoutmanaer重写 smoothScrollToPosition方法
参照:源码: smoothScrollToPosition

package com.google.android.material.datepicker;

import android.content.Context;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;
import android.util.DisplayMetrics;

/**
 * Layout manager for {@link MaterialCalendar} that slows the scroll down to appear smoother for
 * months.
 */
class SmoothCalendarLayoutManager extends LinearLayoutManager {

  /** Default value in {@link LinearSmoothScroller} is 25f */
  private static final float MILLISECONDS_PER_INCH = 100f;

  SmoothCalendarLayoutManager(Context context, int orientation, boolean reverseLayout) {
    super(context, orientation, reverseLayout);
  }

  @Override
  public void smoothScrollToPosition(
      RecyclerView recyclerView, RecyclerView.State state, int position) {
    final LinearSmoothScroller linearSmoothScroller =
        new LinearSmoothScroller(recyclerView.getContext()) {

          @Override
          protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
          }
        };
    linearSmoothScroller.setTargetPosition(position);
    startSmoothScroll(linearSmoothScroller);
  }
}

   GridLayoutManager manager = new GridLayoutManager(getContext(), 2, LinearLayoutManager.VERTICAL, false){
            @Override
            public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
                super.smoothScrollToPosition(recyclerView, state, position);
                LinearSmoothScroller smoothScroller =
                        new LinearSmoothScroller(recyclerView.getContext()) {
                            @Override
                            protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                                // 返回:滑过1px时经历的时间(ms)。
                                return 150f / displayMetrics.densityDpi;
                            }

                            @Override
                            public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
                                return boxStart  - viewStart;
                            }
                        };

                smoothScroller.setTargetPosition(position);
                startSmoothScroll(smoothScroller);
            }
        };
AndroidRecyclerView 中,`scrollToPosition` 和 `smoothScrollToPosition` 都可用于滚动到列表指定位置,但存在明显区别。 触发监听器方面,`scrollToPosition` 不会触发 `scrollListener`,而 `smoothScrollToPosition` 会触发 `scrollListener`。在监听 `smoothScrollToPosition` 滑动动画停止时,可添加并处理 `OnScrollListener` 来响应动画结束事件,如以下代码示例展示了如何监听 `smoothScrollToPosition` 滑动动画停止: ```java private void scrollToPosition(int position){ recyclerView.removeOnScrollListener(onScrollListener); recyclerView.addOnScrollListener(onScrollListener); recyclerView.smoothScrollToPosition(position); } RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { switch (newState) { case SCROLL_STATE_IDLE: //we reached the target position recyclerView.removeOnScrollListener(this); break; } } }; ``` 滚动效果方面,`scrollToPosition` 是直接将指定位置的项滚动到可见区域,没有滚动动画效果,会瞬间定位到目标位置;`smoothScrollToPosition` 则带有平滑滚动的动画效果,会在一定时间内逐渐滚动到指定位置,给用户更流畅的视觉体验。 调用有效性方面,使用 `scrollToPosition` 时,有时在调用该方法前 RecyclerView 还未完全刷新,可能导致该方法无效,此时建议使用 `post` 方法将要滚动的位置移动到 UI 线程队列末尾,示例代码如下: ```java recyclerView.post(new Runnable() { @Override public void run() { recyclerView.scrollToPosition(position); } }); ``` 而 `smoothScrollToPosition` 不存在此类因刷新不及时导致无效问题,调用相对更稳定。[^2][^4][^5]
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zz白龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值