RecyclerView.smoothScrollToPosition(int);滑动过程很突兀,很快就滑动到了指定位置,并没有像函数名那样smooth,本文将分享如何使得RecyclerView平滑流畅的滑动到指定位置。
背景
在项目中,想使RecyclerView慢慢的平缓滑动指定位置,于是使用:
1 | RecyclerView.smoothScrollToPosition(int); |
发现效果并不理想,滑动过程很突兀,很快就滑动到了指定位置,并没有像函数名那样smooth(流畅的,平滑的),也就是说smoothScrollToPosition没有滑动效果,黑人问号???
本文原创作者MichaelX,博客链接:http://blog.csdn.net/xiong_it,转载请注明出处。
探索历程
既然函数名是流畅平缓的滑动到指定位置,为什么并不理想呢?查看源码如下:
1 | public void smoothScrollToPosition(int position) { |
所以实际上是调用RecyclerView.LayoutManager.smoothScrollToPosition()
方法,这是个抽象方法。由于笔者项目中是LinearLayoutManager
于是找到其具体实现如下:
1 | @Override |
生成一个RecyclerView.SmoothScroller
的子类LinearSmoothScroller
对象smoothScroller,接着利用smoothScroller去完成剩下的滑动工作。
于是进去LinearSmoothScroller看看。重大发现—里面有一个跟滑动速度相关的函数:
1 | /** |
既然RecyclerView.smoothScrollToPosition(int);
很快,是不是延长其滑动时间就可以呢?
解决smoothScrollToPosition无效
为了验证上节延长滑动时间的想法,自定义一个LinearLayoutManager:
1 | public class SmoothScrollLayoutManager extends LinearLayoutManager { |
Binggo!成功!调用RecyclerView.smoothScrollToPosition(int);
发现滑动速度变慢很多,不再突兀,不再突然滑过去,没有任何过渡,而是缓慢滑过去,终于名副其实的smooth。
本文原创作者MichaelX,博客链接:http://blog.csdn.net/xiong_it,转载请注明出处。
结语
通过自定义LinearLayoutManager,重写smoothScrollToPosition()方法中LinearSmoothScroller
对象的calculateSpeedPerPixel(DisplayMetrics)
方法,可以使RecyclerView.smoothScrollToPosition(int);
平滑的流畅的滑动到指定位置。其他LayoutManager暂时没用到,需要读者自己尝试。
如果读者朋友们有其他的办法,欢迎留言交流。