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

相关文章

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

MySQL的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq

Redis 的 SUBSCRIBE命令详解

《Redis的SUBSCRIBE命令详解》Redis的SUBSCRIBE命令用于订阅一个或多个频道,以便接收发送到这些频道的消息,本文给大家介绍Redis的SUBSCRIBE命令,感兴趣的朋友跟随... 目录基本语法工作原理示例消息格式相关命令python 示例Redis 的 SUBSCRIBE 命令用于订

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java