smoothScrollToPosition去滚动到一个位置,如果这个位置和当前位置相差很远,比如说300项,你会发现整个过程很长,比如说我遇到的,滚动300项,用了3.5秒。


这主要跟RecyclerView smoothScroll的方式有关,它内部有一个常量值代表每滚动1px需要多少时间,所以滚动的距离越远,需要的时间越长。所以当真的需要滚动很多项时,有时看着真心蛋疼,所以就想看看怎么改一下。

LinearLayoutmanager.smoothScrollToPosition:

@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
        int position) {
    LinearSmoothScroller linearSmoothScroller =
            new LinearSmoothScroller(recyclerView.getContext()) {
                @Override
                public PointF computeScrollVectorForPosition(int targetPosition) {
                    return LinearLayoutManager.this
                            .computeScrollVectorForPosition(targetPosition);
                }
            };
    linearSmoothScroller.setTargetPosition(position);
    startSmoothScroll(linearSmoothScroller);
}

computeScrollVectorForPosition方法不用管,直接复制过来就行了,因为LinearSmoothScroller是抽象类,这个方法必须实现。LinearSmoothScroller 的方法,发现一个方法叫protected int calculateTimeForScrolling(int dx),看这名字就知道函数的作用了,直接重写这个函数,让它最多返回1500:

@Override
protected int calculateTimeForScrolling(int dx) {
    return Math.min(1500, super.calculateTimeForScrolling(dx));
}

smoothScrollToPosition需要的时间的,实际情况时,当实际需要滚动的距离大于10000时,滚动会分多次进行,比如说滚动52000距离,实际会这个函数会调用6次,dx的值前5次是10000,最后一次是2000。实际滚动时间是这6次返回值的和。

知道了这个,解决也简单了,它想分多次调用就让它多次调用吧,我只要每次返回的时间值很小就行了。方法有两个。

  • 直接修改返回值,让它足够小
  • 修改传入的参数,当dx足够小时,计算出的时间自然就小了。

我决定采用第二种方法,当滚动距离比较小时不会造成影响,完整代码如下:

mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) {
    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        LinearSmoothScroller linearSmoothScroller =
                new LinearSmoothScroller(recyclerView.getContext()) {
                    @Override
                    protected int calculateTimeForScrolling(int dx) {
                        // 此函数计算滚动dx的距离需要多久,当要滚动的距离很大时,比如说52000,
                        // 经测试,系统会多次调用此函数,每10000距离调一次,所以总的滚动时间
                        // 是多次调用此函数返回的时间的和,所以修改每次调用该函数时返回的时间的
                        // 大小就可以影响滚动需要的总时间,可以直接修改些函数的返回值,也可以修改
                        // dx的值,这里暂定使用后者.
                        // (See LinearSmoothScroller.TARGET_SEEK_SCROLL_DISTANCE_PX)
                        if (dx > 3000) {
                            dx = 3000;
                        }
                        return super.calculateTimeForScrolling(dx);
                    }

                    @Override
                    public PointF computeScrollVectorForPosition(int targetPosition) {
                        return mLayoutManager.computeScrollVectorForPosition(targetPosition);
                    }
                };
        linearSmoothScroller.setTargetPosition(position);
        startSmoothScroll(linearSmoothScroller);
    }
};