让你明明白白的使用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

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注