关于RecyclerView滑动速度和抛掷速度的控制和改变
2016-09-18 13:34
363 查看
这是我发布的第一篇技术博客,本应该在上个月做项目的时候实时发布的,那时候的理解也最深刻。由于个人执行力不够,拖延到现在,万事开头难,开始了就要坚持下去。
这篇文章主要讲在Android的RecyclerView组件中,如何设置RecyclerView滑动的速度,以及快速滑动(抛掷)后RecyclerView滑行的速度(或者称为距离)。
RecyclerView滑动速度的设置(此处以横向的滑动为例)
自定义一个类继承自LayoutManager,然后重载其scrollHorizontallyBy()方法,其中在该方法中,第一个参数dx即为滑动的距离,因此改变这个值就可以改变滑动的速度。为了方便设置其滑动的速度,可以自定义一个速度因子speedRatio,通过利用dx*speedRatio来达到控制速度的目的。示例代码如下:
而后,实例化这个类,并设置为RecyclerView的布局,代码如下所示
RecyclerView抛掷速度的设置(此处以横向的滑动为例)
自定义一个类继承自RecyclerView,然后重载其fling()方法,在该方法中velocityX为其横向的移动距离,velocityY为其纵向的移动距离(此处以横向的滑动为例),改变这两个参数,即可以改变其相应方向滑动的距离。为了方便设置,这里同样引入一个缩放因子scale,代码示例如下:
public class CustomRecyclerView extends RecyclerView {
private double scale; //抛掷速度的缩放因子
}
而后,在RecyclerView中设置其缩放因子即可,代码如下:
总结
本文介绍了如何改变一个RecyclerView的滑动速度和抛掷速度的问题。通常情况下可能并不需要对这两个参数做出改变,因为我在做项目的时候,用到了5个RecyclerView放置在同一个Activity中,同时又要求每一个RecyclerView的滑动速度不同,因此总结出了以上的方法,实现了相应的需求。同时文章对更多的细节并没有过多的研究,还望批评指正。
文/dukecui(简书作者)
原文链接:http://www.jianshu.com/p/f579999723ca
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
这篇文章主要讲在Android的RecyclerView组件中,如何设置RecyclerView滑动的速度,以及快速滑动(抛掷)后RecyclerView滑行的速度(或者称为距离)。
RecyclerView滑动速度的设置(此处以横向的滑动为例)
自定义一个类继承自LayoutManager,然后重载其scrollHorizontallyBy()方法,其中在该方法中,第一个参数dx即为滑动的距离,因此改变这个值就可以改变滑动的速度。为了方便设置其滑动的速度,可以自定义一个速度因子speedRatio,通过利用dx*speedRatio来达到控制速度的目的。示例代码如下:
public class CustomSGLayoutManager extends StaggeredGridLayoutManager { private double speedRatio; public CustomSGLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } public CustomSGLayoutManager(int spanCount, int orientation) { super(spanCount, orientation); } @Override public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) { int a = super.scrollHorizontallyBy((int)(speedRatio*dx), recycler, state);//屏蔽之后无滑动效果,证明滑动的效果就是由这个函数实现 if(a == (int)(speedRatio*dx)){ return dx; } return a; } public void setSpeedRatio(double speedRatio){ this.speedRatio = speedRatio; } }
而后,实例化这个类,并设置为RecyclerView的布局,代码如下所示
private RecyclerView skyRecyclerView; public void doSomething(){ CustomSGLayoutManager skyLayoutManager = new CustomSGLayoutManager(1,StaggeredGridLayoutManager.HORIZONTAL);//实例化自定义类 skyLayoutManager.setSpeedRatio(0.82);//设置其速度因子 skyRecyclerView.setLayoutManager(skyLayoutManager); }
RecyclerView抛掷速度的设置(此处以横向的滑动为例)
自定义一个类继承自RecyclerView,然后重载其fling()方法,在该方法中velocityX为其横向的移动距离,velocityY为其纵向的移动距离(此处以横向的滑动为例),改变这两个参数,即可以改变其相应方向滑动的距离。为了方便设置,这里同样引入一个缩放因子scale,代码示例如下:
public class CustomRecyclerView extends RecyclerView {
private double scale; //抛掷速度的缩放因子
public CustomRecyclerView(Context context) { super(context); } public CustomRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void setflingScale(double scale){ this.scale = scale; } @Override public boolean fling(int velocityX, int velocityY) { velocityX *= scale; return super.fling(velocityX, velocityY); }
}
而后,在RecyclerView中设置其缩放因子即可,代码如下:
skyLayoutManager.setSpeedRatio(0.5);
总结
本文介绍了如何改变一个RecyclerView的滑动速度和抛掷速度的问题。通常情况下可能并不需要对这两个参数做出改变,因为我在做项目的时候,用到了5个RecyclerView放置在同一个Activity中,同时又要求每一个RecyclerView的滑动速度不同,因此总结出了以上的方法,实现了相应的需求。同时文章对更多的细节并没有过多的研究,还望批评指正。
文/dukecui(简书作者)
原文链接:http://www.jianshu.com/p/f579999723ca
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
相关文章推荐
- Android开发模板------ViewPager(三):实现无限循环、可嵌套RecyclerView、可控制滑动速度
- RecyclerView调用smoothScrollToPosition() 控制滑动速度
- RecyclerView通过manager.smoothScrollToPosition()来改变滑动速度
- 改变ViewPager滑动速度
- 怎样控制viewpager的滑动速度
- RecyclerView实现滚动滑动以及点击改变控件颜色
- viewpager广告位滑动速度控制
- Android——UI篇:ViewPager轮播图控制滑动速度
- 关于RecyclerView缓慢滑动到指定位置
- 改变viewPager滑动的速度,利用反射完美实现
- 关于RecyclerView实现瀑布流,上下滑动时item之间互换位置的问题
- 关于NestedScrollView + RecyclerView页面载入时总是滑动到最底部
- [置顶] viewPager控制滑动速度和时间
- 改变viewpager滑动速度
- RecyclerView 平滑滚动可控制滚动速度 及 滚动的距离-终极解决方案
- 关于recyclerView水平滑动和垂直滑动时的崩溃问题
- 教你控制 RecyclerView 滑动的节奏
- 关于recyclerView快速上下滑动时的崩溃问题
- 关于RecyclerView与ListView控件滑动距离的计算方法
- 关于RecyclerView实现瀑布流,上下滑动时item之间互换位置的问题