前言:

使用RecyclerView时,调用smoothScrollToPostion()方法滑动到指定位置,但是条目很多时滑动的很慢,本篇文章就是实现RecyclerView的快速滑动。

先介绍如何实现,然后再介绍原理。

1. 实现代码

创建FastScrollLinearLayoutManager,继承LinearLayoutManager

复写smoothScrollToPosition()方法,主要复写LinearSmoothScroller中方法

代码如下,解释全在注释中:

public class FastScrollLinearLayoutManager extends LinearLayoutManager {

public FastScrollLinearLayoutManager(Context context) {

super(context);

}

@Override

public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {

LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {

@Override

public PointF computeScrollVectorForPosition(int targetPosition) {

return FastScrollLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition);

}

//该方法控制速度。

//if returned value is 2 ms, it means scrolling 1000 pixels with LinearInterpolation should take 2 seconds.

@Override

protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {

/*

控制单位速度, 毫秒/像素, 滑动1像素需要多少毫秒.

默认为 (25F/densityDpi) 毫秒/像素

mdpi上, 1英寸有160个像素点, 25/160,

xxhdpi,1英寸有480个像素点, 25/480,

*/

//return 10F / displayMetrics.densityDpi;//可以减少时间,默认25F

return super.calculateSpeedPerPixel(displayMetrics);

}

//该方法计算滑动所需时间。在此处间接控制速度。

//Calculates the time it should take to scroll the given distance (in pixels)

@Override

protected int calculateTimeForScrolling(int dx) {

/*

控制距离, 然后根据上面那个方(calculateSpeedPerPixel())提供的速度算出时间,

默认一次 滚动 TARGET_SEEK_SCROLL_DISTANCE_PX = 10000个像素,

在此处可以减少该值来达到减少滚动时间的目的.

*/

//间接计算时提高速度,也可以直接在calculateSpeedPerPixel提高

if (dx > 3000) {

dx = 3000;

}

int time = super.calculateTimeForScrolling(dx);

LogUtil.d(time);//打印时间看下

return time;

}

};

linearSmoothScroller.setTargetPosition(position);

startSmoothScroll(linearSmoothScroller);

}

}复制代码

从复写的两个方法可以看出,都是为了提高滑动速度。一种是直接修改速度,另外一种是通过减少距离来减少所需时间,间接提高滑动速度。

这两种方法都可以,看自己所需。接下来就讲讲实现的原理。这块我只是梳理的大致的流程,不过至此你已经可以实现快速滑动了。

2. RecyclerView 滑动过程梳理

1.调用RecyclerView.smoothScrollToPosition(position)时

public void smoothScrollToPosition(int position) {

//...直接调用了LayoutManager的该方法

mLayout.smoothScrollToPosition(this, mState, position);

}复制代码

2.LinearLayoutManager中

@Override

public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,

int position) {

LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {

//...

};

//设置终点位置

linearSmoothScroller.setTargetPosition(position);

//开始滚动,使用LinearSmoothScroller(一直匀速滑动,当targetPosition出现在屏幕上时再减速滑动),startSmoothScroll()是LayoutManager中的方法

startSmoothScroll(linearSmoothScroller);

}复制代码

3.LayoutManager中

public void startSmoothScroll(SmoothScroller smoothScroller) {

//...

mSmoothScroller = smoothScroller;

//调用SmoothScroller.start()方法开始滚动,this参数指当前LayoutManager

mSmoothScroller.start(mRecyclerView, this);

}复制代码

4.SmoothScroller中

void start(RecyclerView recyclerView, LayoutManager layoutManager) {

//...

//使用ViewFlinger进行动画,ViewFlinger实现了Runnable接口,并且内部使用了Scroller,这样就可以post自己进而对RecyclerView不断layout就可以实现滑动

mRecyclerView.mViewFlinger.postOnAnimation();

}复制代码

5.ViewFlinger中,这是实现滑动的重点,省略了很多代码逻辑

private class ViewFlinger implements Runnable {

@Override

public void run() {

if (scroller.computeScrollOffset()) {

//调用SmoothScroller的onAnimation方法

smoothScroller.onAnimation(dx - overscrollX, dy - overscrollY);

}

}

}复制代码

6.SmoothScroller中

private void onAnimation(int dx, int dy) {

//...

if (mTargetView != null) {

// verify target position

if (getChildPosition(mTargetView) == mTargetPosition) {

//要滑动到的位置已经显示在屏幕上,onTargetFound()方法里update了差值器,由线性差值器变成了减速的差值器。

onTargetFound(mTargetView, recyclerView.mState, mRecyclingAction);

mRecyclingAction.runIfNecessary(recyclerView);

}

//...

if (mRunning) {

//再下一次滑动

onSeekTargetStep(dx, dy, recyclerView.mState, mRecyclingAction);

//调用内部类Action的runIfNecessary方法

mRecyclingAction.runIfNecessary(recyclerView);

}

}复制代码

7.Action中

private void runIfNecessary(RecyclerView recyclerView) {

//调用了ViewFlinger.smoothScrollBy()方法,并传入了mDuration,mDuration是在SmoothScroller中upDate()时传入的,就是由前文讲的两个方法共同决定的

recyclerView.mViewFlinger.smoothScrollBy(mDx, mDy, mDuration, mInterpolator);

}复制代码

8.ViewFlinger中开始滚动

public void smoothScrollBy(int dx, int dy, int duration, Interpolator interpolator) {

if (mInterpolator != interpolator) {

mInterpolator = interpolator;

mScroller = ScrollerCompat.create(getContext(), interpolator);

}

setScrollState(SCROLL_STATE_SETTLING);

mLastFlingX = mLastFlingY = 0;

//调用Scroller开始滚动,此处即duration

mScroller.startScroll(0, 0, dx, dy, duration);

postOnAnimation();

}复制代码

这块粗略的按照流程说了一下滚动过程,涉及的类比较多,最终通过Scroller来进行滚动。

结语:

本篇文章实现了RecyclerView的快速滚动,但需要注意一个问题:如果你的Item比较复杂,滚动起来会卡顿。 这个在看源码时的一个注释里面有提到,后来实践时确实也发现。不得不说微信朋友圈滑动起来的真的快,它用的是ListView,貌似开启了FastEnable属性。

同时也可以仿照知乎,先使用RecyclerView.scrollToPosition(position)直接滑动到某一个位置后再使用smoothScrollToPosition(0)滑动到顶部。这块在RecyclerView里的Action类中jumpTo()的注释里有提到,如果很远的话可以先到一个位置后再滑动。

这两种滑动到顶部的方式都实现了一个小Demo。测试代码在GitHub上 FastScrollFragment 。

另外在自己写的小项目上也用上了 ZhihuDaily,可以查看这两个Demo来具体了解。

android 快速 顶部,Android RecyclerView 快速滑到顶部相关推荐

  1. android快速填表,Android 11将自动填表功能整合到键盘输入建议中

    这一点终于在Android 11中得到了解决,你不用再为了选择你想要填写的密码或信用卡信息而到处找信用卡了. 自动填写系统通常的工作方式是,在填写密码.信用卡信息.发货信息等内容时,你必须在需要填写此 ...

  2. imx6 android快速启动,android启动不起来(已解决)

    我使用imx6dl,使用JB4.3.3-1.1.0版本,但在启动的时候最后停在了Freeing init memory: 232K,就没了动静,各位大侠帮忙分析一下吧: 以下是log: U-Boot ...

  3. Android之解决NestedScrollView嵌套RecyclerView部分手机返回到这个页面Recyclerview顶部,而不是页面NestedScrollView顶部

    1.问题 NestedScrollView嵌套Recyclerview部分手机返回到这个页面Recyclerview顶部,而不是页面NestedScrollView顶部 部分布局大致如下 <an ...

  4. Android快速入门之滚动控件RecyclerView

    之前已经用过了ListView控件,虽然可以实现许多放入功能,但是其扩展性还是有一定缺陷的,比如所无法实现横向布局,为此,Android中提供了一个更强大的滚动控件--RecyclerView,它可以 ...

  5. Android | ListView、RecyclerView 实现一键回到顶部

    一.ListView 1.定义 /*** 列表视图*/private ListView lvPhotoList;/*** 滑动到顶部按钮*/private FloatingActionButton f ...

  6. Android快速开发整理(库、插件,kotlin枚举注解

    RxAndroid implementation 'io.reactivex.rxjava2:rxandroid:2.1.1' Github:https://github.com/ReactiveX/ ...

  7. Android快速入门

    第一天 Android快速入门 第一章快速入门............................................................................. ...

  8. android 快速启动,《Android APP可以有的东西》之显示篇:快速启动App

    前言 又有好久木有更新啦,快成咸鱼了. 早上看到一篇关于app快速启动的公众号文章,觉得应该全面了解一下这个东西,所以这篇文章就小小地实践一下下记录下来,内容并不多,demo都没有必要上,大家直接看代 ...

  9. Android view.settran,Android RecyclerView从入门到玩坏

    目录 前言 基础使用 分隔线 点击监听 搭配CardView 更丰富的条目 增删条目 快速添加视图 让RecyclerView支持复杂视图 最后前言 RecyclerView在Android界面开发当 ...

最新文章

  1. 西湖大学鞠峰组:环境微生物的宏基因组学实例与新发现
  2. Spring事务——Spring 2.X的事务配置策略
  3. Appium之Hybrid APP混合应用测试
  4. Python机器学习--回归
  5. 六十八、SpringBoot连接MongoDB操作
  6. QLineEdit学习
  7. 使用Maven,Jetty和Tomcat在嵌入式容器中运行Java Web应用程序
  8. java中使用json import_JAVA中使用JSON
  9. mac电脑上的效率工具:alfred 4
  10. phpcms模板标签整理
  11. 服务器性能测试 iometer 测试io
  12. STATA画图命令(一)
  13. 正确使用计算机键盘的方法是,电脑键盘指法练习的方法
  14. 大数据分析:家庭教育的10个主要问题
  15. AMR NB格式解析
  16. 大数据Hive学习案例(2)——基于汽车销售的日志数据分析
  17. 实用帖!推荐一个无版权、免费、高清图片素材网站!
  18. 什么是透传模块?为什么要透传?
  19. linux内核态加速文件读取,学习在kernel态下使用NEON对算法进行加速的方法
  20. http协议与https协议+UDP协议和TCP协议+WebSocket协议下服务端主动去发送信息+对称加密与非对称加密+get和post请求方式区别详解+浏览器内核以及jsj解析引擎

热门文章

  1. matlab编写识别手写数字_用于图像识别的五大最佳编程语言!
  2. 生产事故 java_记一次生产事故:30万单就这样没了!
  3. SharedPreferences记住用户密码 态判断应用是否首次启动等
  4. mac linux工具下载,xshell mac版
  5. 聚类算法 sklearn k_means (返回一维数据的最优聚类)
  6. 107. Leetcode 123. 买卖股票的最佳时机 III (动态规划-股票交易)
  7. 知识图谱学习笔记-非结构化数据处理
  8. 搭建Keras,TensorFlow运行环境
  9. 【在CSDN创作2021年度总结】2021年的第一场雪,来的比以往更早一些
  10. 深度5万字好文:Python应用实战案例-带你深入理解Matplotlib