让你明明白白的使用RecyclerView——SnapHelper详解

来源:互联网 发布:mac同步的照片在哪里 编辑:程序博客网 时间:2024/05/18 02:20

一、前言

Google最新发布的support v4包更新到24.2.0,由原来的一个大包分割成多个小module。这样做真是太贴心不过了,以后不会再因为单独使用某一个功能而将整个v4包导入项目中,而是我想用哪个就导入哪个,很大程度上减小了APK的大小。

com.android.support:support-compat:24.2.0 
com.android.support:support-core-utils:24.2.0 
com.android.support:support-core-ui:24.2.0 
com.android.support:support-media-compat:24.2.0 
com.android.support:support-fragment:24.2.0

SnapHelper就是这次更新里面的一个,其实它是对RecyclerView功能的一种拓展。 
想要详细了解其他更新的,可以点击这个链接

二、SnapHelper介绍

SnapHelper的实现原理是监听RecyclerView.OnFlingListener中的onFling接口。LinearSnapHelper是抽象类SnapHelper的具体实现。 
通过LinearSnapHelper,可以使RecyclerView实现类似ViewPager的功能,无论怎么滑动最终停留在某页正中间。 
区别就在于,ViewPager一次只能滑动一页,RecyclerView+SnapHelper方式可以实现一次滑动好几页。

三、实现效果

这里主要是介绍实现两种效果。

  • 自带的LinearSnapHelper实现 
    可以看到类似ViewPager,将某页居中显示,实现也是很简单,只要下面的两行代码
        LinearSnapHelper mLinearSnapHelper = new LinearSnapHelper();        mLinearSnapHelper.attachToRecyclerView(mRecyclerView);
  • 1
  • 2

这里写图片描述

  • 自定义SnapHelper实现

    既然可以居中显示,那我们能不能让它横向左对齐显示呢,答案当然是可以的,这就需要我们自己定义一个左对齐的SnapHelper。 
    先来看个效果,o(∩_∩)o 哈哈。 
    这里写图片描述

四、实现过程

SnapHelper 是一个抽象类,直接继承需要实现三个方法:

  1. 当拖拽或滑动结束时会回调该方法,返回一个out = int[2],out[0]x轴,out[1] y轴 ,这个值就是需要修正的你需要的位置的偏移量 
    public abstract int[] calculateDistanceToFinalSnap(@NonNull LayoutManager layoutManager, @NonNull View targetView);

  2. 上面方法有一个targetView吧 就是这个方法返回的 
    public abstract View findSnapView(LayoutManager layoutManager);

  3. 用于Fling,根据速度返回你要滑到的position 
    public abstract int findTargetSnapPosition(LayoutManager layoutManager, int velocityX, int velocityY);

但是,我们不直接继承SnapHelper,而是继承它的实现类LinearSnapHelper,代码如下:

/** * Created by hiwhitley on 2016/9/4. */public class MySnapHelper extends LinearSnapHelper {    private OrientationHelper mHorizontalHelper;    @Nullable    @Override    public int[] calculateDistanceToFinalSnap(RecyclerView.LayoutManager layoutManager, View targetView) {        int[] out = new int[2];        if (layoutManager.canScrollHorizontally()) {            out[0] = distanceToStart(targetView, getHorizontalHelper(layoutManager));        } else {            out[0] = 0;        }        return out;    }    private int distanceToStart(View targetView, OrientationHelper helper) {        return helper.getDecoratedStart(targetView) - helper.getStartAfterPadding();    }    @Nullable    @Override    public View findSnapView(RecyclerView.LayoutManager layoutManager) {        return findStartView(layoutManager, getHorizontalHelper(layoutManager));    }    private View findStartView(RecyclerView.LayoutManager layoutManager,                               OrientationHelper helper) {        if (layoutManager instanceof LinearLayoutManager) {            int firstChild = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();            int lastChild = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();            if (firstChild == RecyclerView.NO_POSITION) {                return null;            }            if (lastChild == layoutManager.getItemCount() - 1) {                return layoutManager.findViewByPosition(lastChild);            }            View child = layoutManager.findViewByPosition(firstChild);            if (helper.getDecoratedEnd(child) >= helper.getDecoratedMeasurement(child) / 2                    && helper.getDecoratedEnd(child) > 0) {                return child;            } else {                return layoutManager.findViewByPosition(firstChild + 1);            }        }        return super.findSnapView(layoutManager);    }    private OrientationHelper getHorizontalHelper(            @NonNull RecyclerView.LayoutManager layoutManager) {        if (mHorizontalHelper == null) {            mHorizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager);        }        return mHorizontalHelper;    }}
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

基本就是参考着自带的LinearSnapHelper实现的, 
这里有几点需要特别注意一下,

第11~24行:我们只考虑横向左对齐,所以只要处理out[0]的值,distanceToStart()方法返回修正的偏移量。

第41~43行:这是为了解决当翻到最后一页的时候,最后一个Item不能完整显示的问题(不信,你可以注释了试试就知道啦)。

            if (lastChild == layoutManager.getItemCount() - 1) {                return layoutManager.findViewByPosition(lastChild);            }
  • 1
  • 2
  • 3

第47~52行:得到此时需要左对齐显示的条目

         if (helper.getDecoratedEnd(child) >= helper.getDecoratedMeasurement(child) / 2                    && helper.getDecoratedEnd(child) > 0) {                return child;            } else {                return layoutManager.findViewByPosition(firstChild + 1);            }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

最后只要用上我们自己的SnapHelper,就可以轻松搞定了。

        MySnapHelper mMySnapHelper = new MySnapHelper();        mMySnapHelper.attachToRecyclerView(mRecyclerView);
  • 1
  • 2

五、源码下载

GitHub下载 
源码下载 
如果您觉得对你有所帮助,欢迎Star和留言,来鼓励一下我。o(∩_∩)o

六、拓展阅读

关于Android24.2.0支持库SnapHelper的使用 
LinearSnapHelper源码解析


//***************************这是我的用法:**************************************
这是我的recyclerView横向滑动位置控制器的地址链接:

GallerySnapHelper

阅读全文
0 0
原创粉丝点击