美文网首页Android从入门到精通Android recycleviewAndroid开发
关于RecyclerView滑动速度和抛掷速度的控制和改变

关于RecyclerView滑动速度和抛掷速度的控制和改变

作者: dukecui | 来源:发表于2016-04-05 16:04 被阅读7334次

    这是我发布的第一篇技术博客,本应该在上个月做项目的时候实时发布的,那时候的理解也最深刻。由于个人执行力不够,拖延到现在,万事开头难,开始了就要坚持下去。
    这篇文章主要讲在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的滑动速度不同,因此总结出了以上的方法,实现了相应的需求。同时文章对更多的细节并没有过多的研究,还望批评指正。

    相关文章

      网友评论

      • 可乐_JS:还是很溜的,这一段能稍微解释下么:
        if(a == (int)(speedRatio*dx)){
        return dx;
        }
        可乐_JS:@柨柨 哦
        柨柨:@js_小可乐 感觉没有判断这个的必要,直接返回就好了
      • firebirds:最后的
        skyLayoutManager.setSpeedRatio(0.5);
        写错了吧
        应该是skyRecyclerView.setflingScale(0.5);

      本文标题:关于RecyclerView滑动速度和抛掷速度的控制和改变

      本文链接:https://www.haomeiwen.com/subject/mcuplttx.html