当前位置: 首页 > news >正文

ViewPager、RecycleView实现轮播图

1.ViewPager实现轮播图形效果。

   1)layout中,PageIndicatorView轮播的View

 <RelativeLayoutandroid:layout_width="match_parent"android:layout_height="200dp"android:orientation="vertical"><androidx.viewpager2.widget.ViewPager2android:id="@+id/viewpager"android:layout_width="match_parent"android:layout_height="match_parent" /><com.example.viewpager.indicator.PageIndicatorViewandroid:id="@+id/indicator1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true" /></RelativeLayout>

   2)Activity中

 List<String> list = new ArrayList<>();for (int i = 0; i < 5; i++) {list.add("pos: " + i);}viewPager = findViewById(R.id.viewpager);indicator1 = findViewById(R.id.indicator1);//设置ViewPager是横向还是纵向滚动viewPager.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);viewPager.setAdapter(new MyPageAdapter2(this, list));indicator1.initIndicator(list.size());viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {@Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {super.onPageScrolled(position, positionOffset, positionOffsetPixels);}@Overridepublic void onPageSelected(int position) {//当页面滑动时,切换指示器if (indicator1.getChildCount() > 0) {indicator1.setSelectedPage(position % indicator1.getChildCount());}}@Overridepublic void onPageScrollStateChanged(int state) {super.onPageScrollStateChanged(state);}});

2.RecycleView实现轮播效果:

  1)Layout中

  <RelativeLayoutandroid:layout_width="match_parent"android:layout_height="200dp"android:layout_marginTop="20dp"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/recycle_view"android:layout_width="match_parent"android:layout_height="200dp"/><com.example.viewpager.indicator.PageIndicatorViewandroid:id="@+id/indicator2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true" /></RelativeLayout>

2)Activity中:

   

   recyclerView = findViewById(R.id.recycle_view);indicator2 = findViewById(R.id.indicator2);indicator2.initIndicator(list.size());LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);recyclerView.setLayoutManager(manager);recyclerView.setAdapter(new MyPageAdapter2(this, list));recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {@Overridepublic void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {super.onScrolled(recyclerView, dx, dy);int firstReal = manager.findFirstVisibleItemPosition();View viewFirst = manager.findViewByPosition(firstReal);float width = viewFirst.getWidth();if (width != 0 && viewFirst != null) {float right = viewFirst.getRight();float ratio = right / width;if (ratio > 0.8) {if (currentIndex != firstReal) {currentIndex = firstReal;indicator2.setSelectedPage(currentIndex);}} else if (ratio < 0.2) {if (currentIndex != firstReal + 1) {currentIndex = firstReal + 1;indicator2.setSelectedPage(currentIndex);}}}}});

3.滑动指示器:

public class PageIndicatorView extends LinearLayout {private Context mContext = null;private int dotSize = 5; // 指示器的大小(dp)private int margins = 5; // 指示器间距(dp)private List<View> indicatorViews = null; // 存放指示器public PageIndicatorView(Context context) {this(context, null);}public PageIndicatorView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public PageIndicatorView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context);}private void init(Context context) {this.mContext = context;setGravity(Gravity.CENTER);setOrientation(HORIZONTAL);dotSize = Utils.dip2px(dotSize);margins = Utils.dip2px( margins);}/*** 初始化指示器,默认选中第一页** @param count 指示器数量,即页数*/public void initIndicator(int count) {if (indicatorViews == null) {indicatorViews = new ArrayList<>();} else {indicatorViews.clear();removeAllViews();}View view;LayoutParams params = new LayoutParams(dotSize, dotSize);params.setMargins(margins, margins, margins, margins);for (int i = 0; i < count; i++) {view = new View(mContext);view.setBackgroundResource(R.drawable.indicator_normal);addView(view, params);indicatorViews.add(view);}if (indicatorViews.size() > 0) {indicatorViews.get(0).setBackgroundResource(R.drawable.indicator_active);}}/*** 设置选中页** @param selected 页下标,从0开始*/public void setSelectedPage(int selected) {for (int i = 0; i < indicatorViews.size(); i++) {if (i == selected) {indicatorViews.get(i).setBackgroundResource(R.drawable.indicator_active);} else {indicatorViews.get(i).setBackgroundResource(R.drawable.indicator_normal);}}}}

  两个小圆点代码:

<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="oval"><solid android:color="#fffc9d16" />
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="oval"><solid android:color="#FFD9D9D9" />
</shape>

代码下载:

https://download.csdn.net/download/niuyongzhi/88408862

相关文章:

ViewPager、RecycleView实现轮播图

1.ViewPager实现轮播图形效果。 1&#xff09;layout中&#xff0c;PageIndicatorView轮播的View <RelativeLayoutandroid:layout_width"match_parent"android:layout_height"200dp"android:orientation"vertical"><androidx.viewpager…...

【FreeRTOS】【STM32】01从零开始的freertos之旅 浏览源码下的文件夹

基于野火以及正点原子 在打开正点原子的资料pdf时&#xff0c;我遇到了pdf无法复制粘贴的问题&#xff0c;这里有个pdf解锁文字复制功能的网址&#xff0c;mark一下。超级pdf 参考资料《STM32F429FreeRTOS开发手册_V1.2》 官方资料 FreeRTOS 的源码和相应的官方书籍均可从官…...

【PPT】ppt里面使用svg图标

要想编辑好的PPT&#xff0c;少不了小图标的美化&#xff0c;图标可以使PPT变得更有趣&#xff0c;更易懂&#xff0c;更美观。 对于png&#xff0c;主要处理它的颜色&#xff0c;可使用【重新着色】功能。 对于jpg&#xff0c;主要处理它的背景&#xff0c;删除背景后同png处…...

uni-app:实现页面效果4(echarts数据可视化)

效果 代码 <template><view><view><view class"title">概况</view><view class"line_position"><view class"line1"><view class"item"><view class"one">今日销售…...

vue实现echarts中 9种 折线图图例

let datas [{ DivideScore: 7, UserScore: 7.2, Name: 目标制定 },{ DivideScore: 7, UserScore: 7, Name: 具体性 },{ DivideScore: 7, UserScore: 7.5, Name: 可衡量性 },{ DivideScore: 7, UserScore: 7, Name: 可实现性 },{ DivideScore: 7, UserScore: 7, Name: 时间限定…...

redis实战-实现用户签到UV统计

BitMap功能演示 我们针对签到功能完全可以通过mysql来完成&#xff0c;比如说以下这张表 用户一次签到&#xff0c;就是一条记录&#xff0c;假如有1000万用户&#xff0c;平均每人每年签到次数为10次&#xff0c;则这张表一年的数据量为 1亿条 每签到一次需要使用&#xff08…...

作为创始人的价值观与心法,构建系统

价值观 绿色、健康、有趣、感恩、谦卑、责任、勇气、客观、冷静、自洽、尊重、价值、服务、善良、利他 作为co-founder衡量的一个很重要的标准就是这个人的人品&#xff0c;大家一起做事情的体验要好&#xff0c;才能有large energy&#xff0c;且流通。 乐观、通达&#xf…...

Go语言基础面经

1.go语言编程的好处是什么 编译和运行都很快。 在语言层级支持并行操作。 有垃圾处理器。 内置字符串和 maps。 函数是 go 语言的最基本编程单位。 2.说说go语言的select机制 select 机制用来处理异步 IO 问题 select 机制最大的一条限制就是每个 case 语句里必须是一个…...

服务器文件备份

服务器上&#xff0c;做好跟应用程序有关的文件备份&#xff08;一般备份到远程的盘符&#xff09;&#xff0c;有助于当服务器发生硬件等故障时&#xff0c;可以对系统进行进行快速恢复。 下面以Windows服务器为例&#xff0c;记录如何做文件的备份操作。 具体操作如下&#…...

剑指offer——JZ68 二叉搜索树的最近公共祖先 解题思路与具体代码【C++】

一、题目描述与要求 二叉搜索树的最近公共祖先_牛客题霸_牛客网 (nowcoder.com) 题目描述 给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。 1.对于该题的最近的公共祖先定义:对于有根树T的两个节点p、q&#xff0c;最近公共祖先LCA(T,p,q)表示一个节点x&#…...

[Spring] @Bean 修饰方法时如何注入参数

目录 一、Bean 的简单使用 1、正常情况 2、问题提出 二、解决方案 1、Qualifier 2、直接写方法名 三、特殊情况 1、DataSource 一、Bean 的简单使用 在开发中&#xff0c;基于 XML 文件配置 Bean 对象的做法非常繁琐且不好维护&#xff0c;因此绝大部分情况下都是使用…...

docker拉取镜像错误 missing signature key

您正在尝试使用 yum 在 CentOS 或 RHEL 系统上安装 docker-ce&#xff0c;但您遇到了一些问题。根据您提供的输出&#xff0c;这里有几个需要注意的点&#xff1a; 系统未注册: "This system is not registered with an entitlement server" 指示您的系统未注册。对于…...

基于可解释性特征矩阵与稀疏采样全局特征组合的人体行为识别

论文还未发表&#xff0c;不细说&#xff0c;欢迎讨论。 Title: A New Solution to Skeleton-Based Human Action Recognition via the combination usage of explainable feature extraction and sparse sampling global features. Abstract: With the development of deep …...

OpenCV4(C++)—— 仿射变换、透射变换和极坐标变换

文章目录 一、仿射变换1. getRotationMatrix2D()2. warpAffine() 二、透射变换三、极坐标变换 一、仿射变换 在OpenCV中没有专门用于图像旋转的函数&#xff0c;而是通过图像的仿射变换实现图像的旋转。实现图像的旋转首先需要确定旋转角度和旋转中心&#xff0c;之后确定旋转…...

http.header.Set()与Add()区别;

在Go语言中进行HTTP请求时&#xff0c;http.Header对象表示HTTP请求或响应的头部信息。http.Header是一个map[string][]string类型的结构&#xff0c;用于存储键值对&#xff0c;其中键表示HTTP头字段的名称&#xff0c;值是一个字符串切片&#xff0c;可以存储多个相同名称的头…...

vue-7-vuex

一、Vuex 概述 目标&#xff1a;明确Vuex是什么&#xff0c;应用场景以及优势 1.是什么 Vuex 是一个 Vue 的 状态管理工具&#xff0c;状态就是数据。 大白话&#xff1a;Vuex 是一个插件&#xff0c;可以帮我们管理 Vue 通用的数据 (多组件共享的数据)。例如&#xff1a;购…...

SSO单点登录和OAuth2.0区别

一、概述 SSO是Single Sign On的缩写&#xff0c;OAuth是Open Authority的缩写&#xff0c;这两者都是使用令牌的方式来代替用户密码访问应用。流程上来说他们非常相似&#xff0c;但概念上又十分不同。SSO大家应该比较熟悉&#xff0c;它将登录认证和业务系统分离&#xff0c…...

【轻松玩转MacOS】基本操作篇

引言 本文是系列的开篇&#xff0c;我将为大家介绍MacOS的基本操作。对于初次接触MacOS的用户来说&#xff0c;掌握这些基本操作是必不可少的。无论是启动和关机&#xff0c;还是使用键盘和鼠标&#xff0c;或者是快捷键的使用&#xff0c;这些基本操作都是你开始使用MacOS的第…...

华为ICT——第三章图像处理基本任务

目录 1&#xff1a;数字图像处理的层次&#xff1a;&#xff08;处理-分析-理解&#xff09;顺序不能错&#xff1a; 2&#xff1a;图像处理&#xff08;图像处理过程&#xff09;&#xff1a; 3&#xff1a;图像分析&#xff08;特征提取&#xff09;&#xff1a; 4&#x…...

(C++)引用的用法总结

引用&#xff08;reference&#xff09;是C极为重要的一部分&#xff0c;本文对其用法进行简单总结。 1. 引用的基本用法 引用的关键字为&&#xff0c;表示取地址的意思&#xff0c;引用变量定义如下&#xff1a; int m 1; int &n m; //定义 cout<<"n:…...

5分钟终极指南:用HunterPie轻松提升《怪物猎人:世界》狩猎效率

5分钟终极指南&#xff1a;用HunterPie轻松提升《怪物猎人&#xff1a;世界》狩猎效率 【免费下载链接】HunterPie-legacy A complete, modern and clean overlay with Discord Rich Presence integration for Monster Hunter: World. 项目地址: https://gitcode.com/gh_mirr…...

Multisim导入自定义三极管S8050/S8550保姆级教程:从SPICE文件到成功仿真

Multisim实战&#xff1a;从零构建S8050三极管模型与仿真验证全流程 在电子电路设计与仿真领域&#xff0c;准确的三极管模型往往是项目成功的关键。许多工程师和爱好者在使用Multisim时都遇到过这样的困境&#xff1a;官方元件库中缺少特定型号的三极管&#xff08;如常见的S8…...

ssm中国篮球人才管理系统(10050)

有需要的同学&#xff0c;源代码和配套文档领取&#xff0c;加文章最下方的名片哦 一、项目演示 项目演示视频 二、资料介绍 完整源代码&#xff08;前后端源代码SQL脚本&#xff09;配套文档&#xff08;LWPPT开题报告/任务书&#xff09;远程调试控屏包运行一键启动项目&…...

ARM Cortex-M调试陷阱:Flash断点残留如何导致Hard Fault

1. 项目概述&#xff1a;一次由断点引发的“血案”与深度剖析最近在支持一个基于NXP KW36&#xff08;Cortex-M0内核&#xff09;的BLE项目时&#xff0c;我遇到了一个极其隐蔽且令人抓狂的问题。同一批次的板子&#xff0c;烧录完全相同的固件&#xff0c;绝大多数运行正常&am…...

终极指南:如何利用Play Integrity API构建专业级Android安全检测工具

终极指南&#xff1a;如何利用Play Integrity API构建专业级Android安全检测工具 【免费下载链接】play-integrity-checker-app Get info about your Device Integrity through the Play Intergrity API 项目地址: https://gitcode.com/gh_mirrors/pl/play-integrity-checker…...

终极Windows解析工具:WinFlexBison完整指南

终极Windows解析工具&#xff1a;WinFlexBison完整指南 【免费下载链接】winflexbison Main winflexbision repository 项目地址: https://gitcode.com/gh_mirrors/wi/winflexbison 你是否在Windows平台上开发编译器、解析器或需要处理复杂文本格式时&#xff0c;为缺少…...

Windows和Office激活难题?3分钟永久激活的智能方案

Windows和Office激活难题&#xff1f;3分钟永久激活的智能方案 【免费下载链接】KMS_VL_ALL_AIO Smart Activation Script 项目地址: https://gitcode.com/gh_mirrors/km/KMS_VL_ALL_AIO 还在为Windows系统频繁弹出激活提示而烦恼吗&#xff1f;Office文档突然变成只读模…...

3分钟掌握ppInk:Windows屏幕标注工具的终极使用指南

3分钟掌握ppInk&#xff1a;Windows屏幕标注工具的终极使用指南 【免费下载链接】ppInk Fork from Gink 项目地址: https://gitcode.com/gh_mirrors/pp/ppInk 你是否在演示时需要用鼠标或触摸屏快速标注屏幕内容&#xff1f;是否希望有一款简单易用但功能强大的标注工具…...

Windows终极优化神器:WinUtil - 一键解决系统安装、优化、修复的完整指南

Windows终极优化神器&#xff1a;WinUtil - 一键解决系统安装、优化、修复的完整指南 【免费下载链接】winutil Chris Titus Techs Windows Utility - Install Programs, Tweaks, Fixes, and Updates 项目地址: https://gitcode.com/GitHub_Trending/wi/winutil 你是否厌…...

如何让Windows资源管理器完美预览iPhone照片:HEIC缩略图插件全解析

如何让Windows资源管理器完美预览iPhone照片&#xff1a;HEIC缩略图插件全解析 【免费下载链接】windows-heic-thumbnails Enable Windows Explorer to display thumbnails for HEIC/HEIF files 项目地址: https://gitcode.com/gh_mirrors/wi/windows-heic-thumbnails 你…...