自定义ViewPager和RecyclerView指示器 Indicator

2024-06-01 16:32

本文主要是介绍自定义ViewPager和RecyclerView指示器 Indicator,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

现在好多App首页都会有这样的条目

在这里插入图片描述
这种的实现方式要么是viewpager翻页滚动,要么就是recyclerView持续滚动。下面的指示器系统的太丑 ,所以就自定义了一个。下面是demo简单的效果图:
在这里插入图片描述
在这里插入图片描述

ViewPagerIndicator

因为嫌麻烦这里的指示器没有用Canvas进行绘制,用了布局文件代替:

指示器布局文件 view_indicator.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/root"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#000000"><Viewandroid:id="@+id/ind_view"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#ff0000"/></FrameLayout>

ViewPagerIndicator.java

public class ViewPagerIndicator extends FrameLayout {private View rootView;private View indView;public ViewPagerIndicator(@NonNull Context context) {this(context, null);}public ViewPagerIndicator(@NonNull Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public ViewPagerIndicator(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);View root = inflate(context, R.layout.app_viewpager_indicator, this);rootView = root.findViewById(R.id.root);indView = root.findViewById(R.id.ind_view);}int indViewWidth = 0;@Overridepublic void draw(Canvas canvas) {super.draw(canvas);}public void setWithViewPager(ViewPager viewPager) {//如果没有adapter,则隐藏不显示if (null == viewPager.getAdapter()) {setVisibility(GONE);Log.e(getClass().getSimpleName(), "no adapter");return;}//获取viewPager中fragment的数量final int count = viewPager.getAdapter().getCount();if (count == 0) {return;}//加载到window之后再进行view宽度的获取rootView.post(new Runnable() {@Overridepublic void run() {//获取当前滑块的宽度indViewWidth = getWidth() / count;FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) indView.getLayoutParams();layoutParams.width = indViewWidth;indView.setLayoutParams(layoutParams);}});viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {/**** @param position* @param positionOffset [0,1]中的值,指示在位置处与页面的偏移百分比。* @param positionOffsetPixels 以像素为单位的值,表示与位置的偏移量。*/@Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {//获取滑块距父布局左侧的距离int left = (int) (position * indViewWidth + positionOffset * indViewWidth);//重新布局滑块viewindView.layout(left, indView.getTop(), left + indViewWidth, indView.getBottom());}@Overridepublic void onPageSelected(int position) {}@Overridepublic void onPageScrollStateChanged(int state) {}});}
}

步骤如下
1.计算出滑块的宽高
2.为viewPager添加滑动监听事件,监听滑动距离,计算出移动距离重新布局滑块

RecyclerViewIndicator

indicator 布局同上

RecyclerViewIndicator.java

public class RecyclerViewIndicator extends FrameLayout {private View rootView;private View indView;public RecyclerViewIndicator(@NonNull Context context) {this(context, null);}public RecyclerViewIndicator(@NonNull Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public RecyclerViewIndicator(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);View root = inflate(context, R.layout.app_viewpager_indicator, this);rootView = root.findViewById(R.id.root);indView = root.findViewById(R.id.ind_view);}int indViewWidth = 0;int indViewHeight = 0;float rate = 0f;/*** 绑定recyclerView* @param recyclerView* @param orientation 排列方向**  这里用到的recyclerView的三个方法*  computeHorizontalScrollExtent/computeVerticalScrollExtent 当前显示在屏幕上的总长度*  computeHorizontalScrollOffset/computeVerticalScrollOffset 当前滑动的总长度*  computeHorizontalScrollRange/computeVerticalScrollRange recylerView内部的总长度*/public void setWithRecyclerView(final RecyclerView recyclerView, int orientation) {final boolean isHorizontal = orientation == RecyclerView.HORIZONTAL;rootView.post(new Runnable() {@Overridepublic void run() {float scrollRange = isHorizontal ? recyclerView.computeHorizontalScrollRange() : recyclerView.computeVerticalScrollRange();float scrollExtent = isHorizontal ? recyclerView.computeHorizontalScrollExtent() : recyclerView.computeVerticalScrollExtent();LayoutParams layoutParams = (LayoutParams) indView.getLayoutParams();if (isHorizontal) {//算出比例rate = (float) getWidth() / scrollRange;//由显示在屏幕上的总长度算出滑块长度indViewWidth = (int) (scrollExtent * rate);layoutParams.height = LayoutParams.MATCH_PARENT;layoutParams.width = indViewWidth;} else {rate = (float) getHeight() / scrollRange;layoutParams.width = LayoutParams.MATCH_PARENT;indViewHeight = (int) (scrollExtent * rate);layoutParams.height = indViewHeight;}indView.setLayoutParams(layoutParams);}});recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {@Overridepublic void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {super.onScrolled(recyclerView, dx, dy);int scrollOffset = isHorizontal ? recyclerView.computeHorizontalScrollOffset() : recyclerView.computeVerticalScrollOffset();//由recyclerView滑动距离算出滑块移动距咯if (isHorizontal) {int left = (int) (scrollOffset * rate);indView.layout(left, indView.getTop(), left + indViewWidth, indView.getBottom());} else {int top = (int) (scrollOffset * rate);indView.layout(indView.getLeft(), top, indView.getRight(), top + indViewHeight);}}});}
}

步骤基本上和recyclerView的实现步骤一致,
这里主要用到了recyclerView的三个方法进行计算

computeHorizontalScrollExtent/computeVerticalScrollExtent 当前显示在屏幕上的总长度
computeHorizontalScrollOffset/computeVerticalScrollOffset 当前滑动的总长度
computeHorizontalScrollRange/computeVerticalScrollRange recylerView内部的总长度

通过 orientation 来区分recyclrView 的排列方式

demo地址

demo只是做了实现的方式,界面有点丑,自己可以更改背景进行修改。也可以是用绘制的方式,添加自定义view的参数。

这篇关于自定义ViewPager和RecyclerView指示器 Indicator的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

C#中通过Response.Headers设置自定义参数的代码示例

《C#中通过Response.Headers设置自定义参数的代码示例》:本文主要介绍C#中通过Response.Headers设置自定义响应头的方法,涵盖基础添加、安全校验、生产实践及调试技巧,强... 目录一、基础设置方法1. 直接添加自定义头2. 批量设置模式二、高级配置技巧1. 安全校验机制2. 类型

SpringBoot AspectJ切面配合自定义注解实现权限校验的示例详解

《SpringBootAspectJ切面配合自定义注解实现权限校验的示例详解》本文章介绍了如何通过创建自定义的权限校验注解,配合AspectJ切面拦截注解实现权限校验,本文结合实例代码给大家介绍的非... 目录1. 创建权限校验注解2. 创建ASPectJ切面拦截注解校验权限3. 用法示例A. 参考文章本文

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并