SnapHelper介绍
一、SnapHelper介绍
SnapHelper是RecyclerView的辅助类,可用来控制在滑动结束后,RecyclerView中item的对齐方式。
SnapHelper是一个抽象类,系统内置了两个默认实现类
-
LinearSnapHelper:
使当前Item居中显示,常用场景是横向的RecyclerView, 类似ViewPager效果,但是又可以快速滑动(滑动多页) -
PagerSnapHelper:
PagerSnapHelper的展示效果和LineSnapHelper是一样的,只是PagerSnapHelper 限制一次只能滑动一页,不能快速滑动。
(SnapHelperxiang解析)[https://blog.csdn.net/huahuahua333686/article/details/125545403]
二、实现效果
这里主要是介绍实现两种效果。
1、自带的LinearSnapHelper实现
可以看到类似ViewPager,将某页居中显示,实现也是很简单,只要下面的两行代码
LinearSnapHelper mLinearSnapHelper = new LinearSnapHelper();
mLinearSnapHelper.attachToRecyclerView(mRecyclerView);
1111.gif
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;
}
}
基本就是参考着自带的LinearSnapHelper实现的,
这里有几点需要特别注意一下,
第11~24行:我们只考虑横向左对齐,所以只要处理out[0]的值,distanceToStart()方法返回修正的偏移量。
第41~43行:这是为了解决当翻到最后一页的时候,最后一个Item不能完整显示的问题(不信,你可以注释了试试就知道啦)。
if (lastChild == layoutManager.getItemCount() - 1) {
return layoutManager.findViewByPosition(lastChild);
}
第47~52行:得到此时需要左对齐显示的条目
if (helper.getDecoratedEnd(child) >= helper.getDecoratedMeasurement(child) / 2
&& helper.getDecoratedEnd(child) > 0) {
return child;
} else {
return layoutManager.findViewByPosition(firstChild + 1);
}
最后只要用上我们自己的SnapHelper,就可以轻松搞定了。
MySnapHelper mMySnapHelper = new MySnapHelper();
mMySnapHelper.attachToRecyclerView(mRecyclerView);
(gitHub源码地址)[https://github.com/hiwhitley/SnapHelperDemo]
参考:https://blog.csdn.net/whitley_gong/article/details/52421215