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

【鸿蒙 HarmonyOS】Swiper组件

一、背景

项目中通常会遇到图片轮播,内容轮播的场景;如:在一些应用首页显示推荐的内容时,需要用到轮播显示的能力。

二、源码地址

✍Gitee开源项目地址👉:https://gitee.com/cheinlu/harmony-os-next-swiper

文档地址👉:文档中心

三、实现效果

  

四、代码示例

@Entry
@Component
struct Index {//轮播图数据源@State swiperList: ResourceStr[] = [$r("app.media.swiper_one"), $r("app.media.swiper_two"), $r("app.media.swiper_three"), $r("app.media.swiper_four"),$r("app.media.swiper_five")]@State selectedIndex: number = 0 //默认选中索引为0build() {Stack({ alignContent: Alignment.Bottom }) {Swiper() {ForEach(this.swiperList, (item: ResourceStr, index: number) => {Image(item).width('100%').height('100%')})}.onChange((index: number) => {this.selectedIndex = index}).autoPlay(true).indicator(false).itemSpace(0).width('100%').height('100%')//此处为自定义角标if (this.swiperList.length > 1) {List({ space: 5 }) {ForEach(this.swiperList, (item: ResourceStr, index: number) => {ListItem() {Image(this.selectedIndex === index ? $r("app.media.carousel_blue") : $r("app.media.carousel_gary")).width(20).aspectRatio(1)}})}.listDirection(Axis.Horizontal).align(Alignment.Center).enableScrollInteraction(false).hitTestBehavior(HitTestMode.Transparent).constraintSize({ maxWidth: '90%' }).height(20).margin({ left: 20, bottom: 20, right: 20 })}}}
}

4.1、属性说明

autoPlay:子组件是否自动播放。默认值:false

indicator:是否启用导航点指示器。默认值:true

itemSpace:设置子组件与子组件之间间隙。默认值:0。说明:不支持设置百分比。

4.2、自定义角标

官方文档默认角标效果:

通过如下代码更改角标样式,更换成菱形图片选中与未选中替换样式

🚀🚀🚀  踩坑不易,还希望各位大佬支持一下

📃 我的土拨鼠开源项目:

✍Gitee开源项目地址👉:https://gitee.com/cheinlu/groundhog-charging-system

✍GitHub开源项目地址👉:https://github.com/cheinlu/groundhog-charging-system

📃 我的鸿蒙NEXT轮播图开源组件:https://gitee.com/cheinlu/harmony-os-next-swiper

最后:👏👏😊😊😊👍👍  

http://www.lryc.cn/news/373949.html

相关文章:

  • 玩具机器人脚本适合场景
  • 人工智能模型组合学习的理论和实验实践
  • MySQL备份与恢复:确保数据的安全与可靠性
  • Noisee AI – AI音乐影片MV在线生成工具,专门为Suno的好搭子来了~
  • 实战计算机网络02——物理层
  • Doris:冷热分层
  • 28.启动与暂停程序
  • 404 页面代码
  • java设计模式和面向对象编程思想
  • 超万卡训练集群网络互联技术解读
  • AtomicInteger类介绍
  • Es 索引查询排序分析
  • 【C语言】解决C语言报错:Format String Vulnerability
  • Python深度学习:Bi-LSTM和LSTM在网络上有什么区别,对比来看
  • Keepalived LVS群集
  • harbor问题总结
  • windows系统,家庭自用NAS。本地局域网 Docker安装nextcloud
  • 迅狐跨境商城系统|全平台兼容|前端采用uni-app跨端框架,后端采用ThinkPHP5框架
  • Elixir学习笔记——进程(Processes)
  • 困惑度作为nlp指标的理解示例
  • 01 Pytorch 基础
  • STL——set、map、multiset、multimap的介绍及使用
  • 使用C语言,写一个类似Linux中执行cat命令的类似功能
  • 【Android】Android系统性学习——Android系统架构
  • 鸿蒙应用开发
  • 索引失效有效的11种情况
  • 字符数组基础知识及题目
  • 一个简单的玩具机器人代码
  • 设计模式-装饰器模式Decorator(结构型)
  • RK3588开发板中使用Qt对zip文件进行解压