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

2024-03-17 22:59

本文主要是介绍让你明明白白的使用RecyclerView——SnapHelper详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、前言

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 mLinearSnapHelper = new LinearSnapHelper();mLinearSnapHelper.attachToRecyclerView(mRecyclerView);
  • 1
  • 2

这里写图片描述

四、实现过程

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@Overridepublic 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@Overridepublic 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下载 
源码下载 
如果您觉得对你有所帮助,欢迎Star和留言,来鼓励一下我。o(∩_∩)o

六、拓展阅读

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


//***************************这是我的用法:**************************************

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

GallerySnapHelper

这篇关于让你明明白白的使用RecyclerView——SnapHelper详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/820454

相关文章

springboot3.x使用@NacosValue无法获取配置信息的解决过程

《springboot3.x使用@NacosValue无法获取配置信息的解决过程》在SpringBoot3.x中升级Nacos依赖后,使用@NacosValue无法动态获取配置,通过引入SpringC... 目录一、python问题描述二、解决方案总结一、问题描述springboot从2android.x

SpringBoot整合AOP及使用案例实战

《SpringBoot整合AOP及使用案例实战》本文详细介绍了SpringAOP中的切入点表达式,重点讲解了execution表达式的语法和用法,通过案例实战,展示了AOP的基本使用、结合自定义注解以... 目录一、 引入依赖二、切入点表达式详解三、案例实战1. AOP基本使用2. AOP结合自定义注解3.

Python中Request的安装以及简单的使用方法图文教程

《Python中Request的安装以及简单的使用方法图文教程》python里的request库经常被用于进行网络爬虫,想要学习网络爬虫的同学必须得安装request这个第三方库,:本文主要介绍P... 目录1.Requests 安装cmd 窗口安装为pycharm安装在pycharm设置中为项目安装req

Python容器转换与共有函数举例详解

《Python容器转换与共有函数举例详解》Python容器是Python编程语言中非常基础且重要的概念,它们提供了数据的存储和组织方式,下面:本文主要介绍Python容器转换与共有函数的相关资料,... 目录python容器转换与共有函数详解一、容器类型概览二、容器类型转换1. 基本容器转换2. 高级转换示

使用Python将PDF表格自动提取并写入Word文档表格

《使用Python将PDF表格自动提取并写入Word文档表格》在实际办公与数据处理场景中,PDF文件里的表格往往无法直接复制到Word中,本文将介绍如何使用Python从PDF文件中提取表格数据,并将... 目录引言1. 加载 PDF 文件并准备 Word 文档2. 提取 PDF 表格并创建 Word 表格

使用Python实现局域网远程监控电脑屏幕的方法

《使用Python实现局域网远程监控电脑屏幕的方法》文章介绍了两种使用Python在局域网内实现远程监控电脑屏幕的方法,方法一使用mss和socket,方法二使用PyAutoGUI和Flask,每种方... 目录方法一:使用mss和socket实现屏幕共享服务端(被监控端)客户端(监控端)方法二:使用PyA

Python使用Matplotlib和Seaborn绘制常用图表的技巧

《Python使用Matplotlib和Seaborn绘制常用图表的技巧》Python作为数据科学领域的明星语言,拥有强大且丰富的可视化库,其中最著名的莫过于Matplotlib和Seaborn,本篇... 目录1. 引言:数据可视化的力量2. 前置知识与环境准备2.1. 必备知识2.2. 安装所需库2.3

HTML5的input标签的`type`属性值详解和代码示例

《HTML5的input标签的`type`属性值详解和代码示例》HTML5的`input`标签提供了多种`type`属性值,用于创建不同类型的输入控件,满足用户输入的多样化需求,从文本输入、密码输入、... 目录一、引言二、文本类输入类型2.1 text2.2 password2.3 textarea(严格

Python数据验证神器Pydantic库的使用和实践中的避坑指南

《Python数据验证神器Pydantic库的使用和实践中的避坑指南》Pydantic是一个用于数据验证和设置的库,可以显著简化API接口开发,文章通过一个实际案例,展示了Pydantic如何在生产环... 目录1️⃣ 崩溃时刻:当你的API接口又双叒崩了!2️⃣ 神兵天降:3行代码解决验证难题3️⃣ 深度

Linux内核定时器使用及说明

《Linux内核定时器使用及说明》文章详细介绍了Linux内核定时器的特性、核心数据结构、时间相关转换函数以及操作API,通过示例展示了如何编写和使用定时器,包括按键消抖的应用... 目录1.linux内核定时器特征2.Linux内核定时器核心数据结构3.Linux内核时间相关转换函数4.Linux内核定时