前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用 RecyclerView 实现 Gallery 画廊效果,并控制 Item 停留位置

使用 RecyclerView 实现 Gallery 画廊效果,并控制 Item 停留位置

作者头像
非著名程序员
发布2018-02-09 15:31:59
3.1K0
发布2018-02-09 15:31:59
举报
文章被收录于专栏:非著名程序员非著名程序员

RecyclerView 作为一个列表滑动控件,我们都知道它既可以横向滑动,也可以竖直滑动,可以实现线性布局管理,瀑布流布局管理,还有 GridView 布局管理。其实我们可以控制其 Item 的停留位置,并使其实现画廊效果。如果大家熟悉 SnapHelper 的话,估计大家就都会了。

什么是 SnapHelper

SnapHelper 的实现原理就是是监听 RecyclerView.OnFlingListener 中的 onFling 接口。support library 中只提供了一个继承类 LinearSnapHelper ,LinearSnapHelper 是抽象类 SnapHelper 的具体实现。 通过 LinearSnapHelper,我们就可以使 RecyclerView 实现类似 ViewPager 的功能,无论怎么滑动最终都会停留在列表页面正中间。

SnapHelper 和 ViewPager 的区别就是 ViewPager 一次只能滑动一页,而 RecyclerView + SnapHelper 的方式可以实现一次滑动好几页。

效果如下:

居中实现方式

使用 SnapHelper 配合 RecyclerView 实现控制 Item 位置居中显示,非常简单,官方默认提供的 LinearSnapHelper 就是居中的,我们直接使用即可。

代码如下:

  1. LinearLayoutManager linearLayoutManager = new
  2. LinearLayoutManager(this,
  3. LinearLayoutManager.HORIZONTAL, false);
  4. recyclerView.setLayoutManager(linearLayoutManager);
  5. new LinearSnapHelper().
  6. attachToRecyclerView(recyclerView);

自定义 SnapHelper

官方提供的默认是居中显示,其实我们也可以自定义,比如:靠左显示,让可见的第一个 Item 居左显示。

效果图如下

自定义 SnapHelper ,一般需要实现两个方法:

  • int[] calculateDistanceToFinalSnap(RecyclerView.LayoutManager layoutManager, View targetView) 当拖拽或滑动结束时会回调该方法,返回一个out = int[2],out[0]x轴,out[1] y轴,这就是我们需要修改的位置偏移量
  • View findSnapView(RecyclerView.LayoutManager layoutManager) 该方法返回上面方法中需要的 targetView 。
代码如下
代码语言:js
复制
public class CustomSnapHelper extends LinearSnapHelper {
 private OrientationHelper mHorizontalHelper;

 @Override
 public int[] calculateDistanceToFinalSnap(RecyclerView.LayoutManager layoutManager, View targetView) {
 int[] out = new int[2];
 //判断支持水平滚动,修改水平方向的位置,是修改的out[0]的值
 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();
 }

 @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);
 //获取偏左显示的Item
 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(
 RecyclerView.LayoutManager layoutManager) {
 if (mHorizontalHelper == null) {
            mHorizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager);
 }
 return mHorizontalHelper;
 }
}

调用自定义的 SnapHelper 代码如下,配合 RecyclerView:

  1. CustomSnapHelper mMySnapHelper = new CustomSnapHelper();
  2. mMySnapHelper.attachToRecyclerView(rv);

最后,其实垂直方向也可以实现哦,大家可以尝试一下垂直方向的使用方式是不是非常简单。

代码 Demo 地址:https://github.com/loonggg/SnapHelperDemo

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-02-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 非著名程序员 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 什么是 SnapHelper
  • 居中实现方式
  • 自定义 SnapHelper
    • 效果图如下
      • 代码如下
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档