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

相关文章

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

HTML5中下拉框<select>标签的属性和样式详解

《HTML5中下拉框<select>标签的属性和样式详解》在HTML5中,下拉框(select标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中选择值的方式,本文将深入探讨select标签的... 在html5中,下拉框(<select>标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中

Rust中的注释使用解读

《Rust中的注释使用解读》本文介绍了Rust中的行注释、块注释和文档注释的使用方法,通过示例展示了如何在实际代码中应用这些注释,以提高代码的可读性和可维护性... 目录Rust 中的注释使用指南1. 行注释示例:行注释2. 块注释示例:块注释3. 文档注释示例:文档注释4. 综合示例总结Rust 中的注释

Python中多线程和多进程的基本用法详解

《Python中多线程和多进程的基本用法详解》这篇文章介绍了Python中多线程和多进程的相关知识,包括并发编程的优势,多线程和多进程的概念、适用场景、示例代码,线程池和进程池的使用,以及如何选择合适... 目录引言一、并发编程的主要优势二、python的多线程(Threading)1. 什么是多线程?2.

Linux使用cut进行文本提取的操作方法

《Linux使用cut进行文本提取的操作方法》Linux中的cut命令是一个命令行实用程序,用于从文件或标准输入中提取文本行的部分,本文给大家介绍了Linux使用cut进行文本提取的操作方法,文中有详... 目录简介基础语法常用选项范围选择示例用法-f:字段选择-d:分隔符-c:字符选择-b:字节选择--c

Java 8 Stream filter流式过滤器详解

《Java8Streamfilter流式过滤器详解》本文介绍了Java8的StreamAPI中的filter方法,展示了如何使用lambda表达式根据条件过滤流式数据,通过实际代码示例,展示了f... 目录引言 一.Java 8 Stream 的过滤器(filter)二.Java 8 的 filter、fi

Rust中的BoxT之堆上的数据与递归类型详解

《Rust中的BoxT之堆上的数据与递归类型详解》本文介绍了Rust中的BoxT类型,包括其在堆与栈之间的内存分配,性能优势,以及如何利用BoxT来实现递归类型和处理大小未知类型,通过BoxT,Rus... 目录1. Box<T> 的基础知识1.1 堆与栈的分工1.2 性能优势2.1 递归类型的问题2.2

使用Go语言开发一个命令行文件管理工具

《使用Go语言开发一个命令行文件管理工具》这篇文章主要为大家详细介绍了如何使用Go语言开发一款命令行文件管理工具,支持批量重命名,删除,创建,移动文件,需要的小伙伴可以了解下... 目录一、工具功能一览二、核心代码解析1. 主程序结构2. 批量重命名3. 批量删除4. 创建文件/目录5. 批量移动三、如何安

springboot的调度服务与异步服务使用详解

《springboot的调度服务与异步服务使用详解》本文主要介绍了Java的ScheduledExecutorService接口和SpringBoot中如何使用调度线程池,包括核心参数、创建方式、自定... 目录1.调度服务1.1.JDK之ScheduledExecutorService1.2.spring

Java使用Tesseract-OCR实战教程

《Java使用Tesseract-OCR实战教程》本文介绍了如何在Java中使用Tesseract-OCR进行文本提取,包括Tesseract-OCR的安装、中文训练库的配置、依赖库的引入以及具体的代... 目录Java使用Tesseract-OCRTesseract-OCR安装配置中文训练库引入依赖代码实