RecyclerView smoothScrollToPosition的滚动时间

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

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

看下LinearLayoutmanager.smoothScrollToPosition:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@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);
}

内部是实例化了一个LinearSmoothScroller的实例来处理Scroll事件的,我们自字义一个LinearSmoothScroller应该就能自定义Smooth事件,上面的computeScrollVectorForPosition方法不用管,直接复制过来就行了,因为LinearSmoothScroller是抽象类,这个方法必须实现。

然后看下LinearSmoothScroller 的方法,发现一个方法叫protected int calculateTimeForScrolling(int dx),看这名字就知道函数的作用了,直接重写这个函数,让它最多返回1500:

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

但是,实际滚动时间更长了。。打Log,把每次返回值和dx的值都打印出来,然后继续测试,然后发现这个函数不是计算这一个smoothScrollToPosition需要的时间的,实际情况时,当实际需要滚动的距离大于10000时,滚动会分多次进行,比如说滚动52000距离,实际会这个函数会调用6次,dx的值前5次是10000,最后一次是2000。实际滚动时间是这6次返回值的和。

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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);
    }
};


原文链接:http://www.angeldevil.me/2015/09/08/The-smoothScrollToPosition-duration-of-RecyclerView/

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值