0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

鸿蒙ArkTS容器组件:Swiper

jf_46214456 来源:jf_46214456 作者:jf_46214456 2024-07-15 09:51 次阅读

Swiper

滑块视图容器,提供子组件滑动轮播显示的能力。

说明:

该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

子组件

可以包含子组件。

说明:
开发前请熟悉鸿蒙开发指导文档 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md]
子组件类型:系统组件和自定义组件,支持渲染控制类型([if/else]、[ForEach]和[LazyForEach])。

Swiper子组件的visibility属性设置为None,Swiper的displayMode属性设置为SwiperDisplayMode.AutoLinear或displayCount属性设置为'auto'时,对应子组件在视窗内不占位,但不影响导航点个数。

Swiper子组件的visibility属性设置为None,或者visibility属性设置为Hidden时,对应子组件不显示,但依然会在视窗内占位。

当Swiper子组件个数小于等于Swiper组件内容区内显示的节点总个数(totalDisplayCount = DisplayCount + prevMargin? (1 : 0 ) + nextMargin? (1 : 0 ))时,按照非循环模式布局处理,此时,前后边距对应子组件不显示,但依然会在视窗内占位。Swiper组件按照totalDisplayCount个数判断测算规格

接口

Swiper(controller?: SwiperController)

参数

参数名参数类型必填参数描述
controller[SwiperController]给组件绑定一个控制器,用来控制组件翻页。

属性

除支持[通用属性]外,还支持以下属性,不支持[Menu控制]。

名称参数类型描述
indexnumber设置当前在容器中显示的子组件的索引值。 默认值:0**说明:**设置小于0或大于等于子组件数量时,按照默认值0处理。 从API version 10开始,该属性支持[$$]双向绑定变量。
autoPlayboolean子组件是否自动播放。 默认值:false**说明:**loop为false时,自动轮播到最后一页时停止轮播。手势切换后不是最后一页时继续播放。
intervalnumber使用自动播放时播放的时间间隔,单位为毫秒。 默认值:3000
indicator10+[DotIndicator][DigitIndicator]
loopboolean是否开启循环。 设置为true时表示开启循环,在LazyForEach懒循环加载模式下,加载的组件数量建议大于5个。 默认值:true
durationnumber子组件切换的动画时长,单位为毫秒。 默认值:400
verticalboolean是否为纵向滑动。 默认值:false
itemSpacenumberstring
displayModeSwiperDisplayMode主轴方向上元素排列的模式,优先以displayCount设置的个数显示,displayCount未设置时本属性生效。 默认值:SwiperDisplayMode.STRETCH
cachedCount8+number设置预加载子组件个数。 默认值:1
disableSwipe8+boolean禁用组件滑动切换功能。 默认值:false
curve8+[Curve]string
displayCount8+numberstring
effectMode8+[EdgeEffect]边缘滑动效果,loop = false时生效。 目前支持的滑动效果参见EdgeEffect的枚举说明。 默认值:EdgeEffect.Spring**说明:**控制器接口调用时不生效回弹。
displayArrow10+value:[ArrowStyle]boolean, isHoverShow?: boolean
nextMargin10+[Length]后边距,用于露出后一项的一小部分。 默认值:0**说明:**仅当SwiperDisplayMode为STRETCH模式时生效。 当主轴方向为横向布局时,nextmargin/prevmargin中任意一个大于子组件测算的宽度,nextmargin和prevmargin均不显示。 当主轴方向为纵向布局时,nextmargin/prevmargin中任意一个大于子组件测算的高度,nextmargin和prevmargin均不显示。
prevMargin10+[Length]前边距,用于露出前一项的一小部分。 默认值:0**说明:**仅当SwiperDisplayMode为STRETCH模式时生效。 当主轴方向为横向布局时,nextmargin/prevmargin中任意一个大于子组件测算的宽度,prevmargin和nextmargin均不显示。 当主轴方向为纵向布局时,nextmargin/prevmargin中任意一个大于子组件测算的高度,nextmargin和prevmargin均不显示。

SwiperDisplayMode枚举说明

名称描述
STRETCH10+Swiper滑动一页的宽度为Swiper组件自身的宽度。
AUTO_LINEAR10+Swiper滑动一页的宽度为视窗内最左侧子组件的宽度。

SwiperController

Swiper容器组件的控制器,可以将此对象绑定至Swiper组件,然后通过它控制翻页。

showNext

showNext(): void

翻至下一页。翻页带动效切换过程,时长通过duration指定。

showPrevious

showPrevious(): void

翻至上一页。翻页带动效切换过程,时长通过duration指定。

finishAnimation

finishAnimation(callback?: () => void): void

停止播放动画。

参数:

参数名参数类型必填项参数描述
callback() => void动画结束的回调。

Indicator10+

设置导航点距离Swiper组件距离。

参数名参数类型必填项参数描述
left[Length]设置导航点距离Swiper组件左边的距离。 默认值:0 单位:vp
top[Length]设置导航点距离Swiper组件顶部的距离。 默认值:0 单位:vp
right[Length]设置导航点距离Swiper组件右边的距离。 默认值:0 单位:vp
bottom[Length]设置导航点距离Swiper组件底部的距离。 默认值:0 单位:vp

DotIndicator10+

圆点指示器属性及功能继承自Indicator。

参数名参数类型必填项参数描述
itemWidth[Length]设置Swiper组件圆点导航指示器的宽,不支持设置百分比。 默认值:6 单位:vp
itemHeight[Length]设置Swiper组件圆点导航指示器的高,不支持设置百分比。 默认值:6 单位:vp
selectedItemWidth[Length]设置选中Swiper组件圆点导航指示器的宽,不支持设置百分比。 默认值:12 单位:vp
selectedItemHeight[Length]设置选中Swiper组件圆点导航指示器的高,不支持设置百分比。 默认值:6 单位:vp
maskboolean设置是否显示Swiper组件圆点导航指示器的蒙版样式。 默认值:false
color[ResourceColor]设置Swiper组件圆点导航指示器的颜色。 默认值:'#182431'(10%透明度)
selectedColor[ResourceColor]设置选中Swiper组件圆点导航指示器的颜色。 默认值:'#007DFF'

DigitIndicator10+

数字指示器属性及功能继承自Indicator。

参数名参数类型必填项参数描述
fontColor[ResourceColor]设置Swiper组件数字导航点的字体颜色。 默认值:'#ff182431'
selectedFontColor[ResourceColor]设置选中Swiper组件数字导航点的字体颜色。 默认值:'#ff182431'
digitFont{ size?:[Length]weight?:number[FontWeight]string }
selectedDigitFont{ size?:[Length] weight?:number[FontWeight]string }

ArrowStyle10+

左右箭头属性。

参数名参数类型必填项参数描述
showBackgroundboolean设置箭头底板是否显示。 默认值:false
isSidebarMiddleboolean设置箭头显示位置。 默认值:false 默认显示在导航点指示器两侧。
backgroundSize[Length]设置底板大小。 在导航点两侧显示: 默认值:24vp 在组件两侧显示: 默认值:32vp 不支持设置百分比。
backgroundColor[ResourceColor]设置底板颜色。 在导航点两侧显示: 默认值:'#00000000' 在组件两侧显示: 默认值:'#19182431'
arrowSize[Length]设置箭头大小。 在导航点两侧显示时: 默认值:18vp 在组件两侧显示时: 默认值:24vp**说明:**showBackground为true时,arrowSize为backgroundSize的3/4。 不支持设置百分比。
arrowColor[ResourceColor]设置箭头颜色。 默认值:'#182431'

SwiperAutoFill10+

自适应属性。

参数名参数类型必填项参数描述
minSize[VP]设置元素显示最小宽度。 默认值:0

事件

除支持[通用事件]外,还支持以下事件:

名称功能描述
onChange(event: (index: number) => void)当前显示的子组件索引变化时触发该事件,返回值为当前显示的子组件的索引值。 - index:当前显示元素的索引。**说明:**Swiper组件结合LazyForEach使用时,不能在onChange事件里触发子页面UI的刷新。
onAnimationStart9+(event: (index: number, targetIndex10+: number, extraInfo10+: [SwiperAnimationEvent]) => void)切换动画开始时触发该回调。 - index:当前显示元素的索引。 - targetIndex:切换动画目标元素的索引。 - extraInfo:动画相关信息,包括主轴方向上当前显示元素和目标元素相对Swiper起始位置的位移,以及离手速度。**说明:**参数为动画开始前的index值(不是最终结束动画的index值),多列Swiper时,index为最左侧组件的索引。
onAnimationEnd9+(event: (index: number, extraInfo: [SwiperAnimationEvent]) => void)切换动画结束时触发该回调。 - index:当前显示元素的索引。 - extraInfo:动画相关信息,只返回主轴方向上当前显示元素相对于Swiper起始位置的位移。**说明:**当Swiper切换动效结束时触发,包括动画过程中手势中断,通过SwiperController调用finishAnimation。参数为动画结束后的index值,多列Swiper时,index为最左侧组件的索引。
onGestureSwipe10+(event: (index: number, extraInfo: [SwiperAnimationEvent]) => void)在页面跟手滑动过程中,逐帧触发该回调。 - index:当前显示元素的索引。 - extraInfo:动画相关信息,只返回主轴方向上当前显示元素相对于Swiper起始位置的位移。**说明:**多列Swiper时,index为最左侧组件的索引。

示例

示例1

// xxx.ets
class MyDataSource implements IDataSource {
  private list: number[] = []

  constructor(list: number[]) {
    this.list = list
  }

  totalCount(): number {
    return this.list.length
  }

  getData(index: number): number {
    return this.list[index]
  }

  registerDataChangeListener(listener: DataChangeListener): void {
  }

  unregisterDataChangeListener() {
  }
}

@Entry
@Component
struct SwiperExample {
  private swiperController: SwiperController = new SwiperController()
  private data: MyDataSource = new MyDataSource([])

  aboutToAppear(): void {
    let list: number[] = []
    for (let i = 1; i <= 10; i++) {
      list.push(i);
    }
    this.data = new MyDataSource(list)
  }

  build() {
    Column({ space: 5 }) {
      Swiper(this.swiperController) {
        LazyForEach(this.data, (item: string) = > {
          Text(item.toString())
            .width('90%')
            .height(160)
            .backgroundColor(0xAFEEEE)
            .textAlign(TextAlign.Center)
            .fontSize(30)
        }, (item: string) = > item)
      }
      .cachedCount(2)
      .index(1)
      .autoPlay(true)
      .interval(4000)
      .indicator(true)
      .loop(true)
      .duration(1000)
      .itemSpace(0)
      .displayArrow({
        showBackground: true,
        isSidebarMiddle: true,
        backgroundSize: 24,
        backgroundColor: Color.White,
        arrowSize: 18,
        arrowColor: Color.Blue
      }, false)
      .curve(Curve.Linear)
      .onChange((index: number) = > {
        console.info(index.toString())
      })
      .onGestureSwipe((index: number, extraInfo: SwiperAnimationEvent) = > {
        console.info("index: " + index)
        console.info("current offset: " + extraInfo.currentOffset)
      })
      .onAnimationStart((index: number, targetIndex: number, extraInfo: SwiperAnimationEvent) = > {
        console.info("index: " + index)
        console.info("targetIndex: " + targetIndex)
        console.info("current offset: " + extraInfo.currentOffset)
        console.info("target offset: " + extraInfo.targetOffset)
        console.info("velocity: " + extraInfo.velocity)
      })
      .onAnimationEnd((index: number, extraInfo: SwiperAnimationEvent) = > {
        console.info("index: " + index)
        console.info("current offset: " + extraInfo.currentOffset)
      })

      Row({ space: 12 }) {
        Button('showNext')
          .onClick(() = > {
            this.swiperController.showNext()
          })
        Button('showPrevious')
          .onClick(() = > {
            this.swiperController.showPrevious()
          })
      }.margin(5)
    }.width('100%')
    .margin({ top: 5 })
  }
}

swiper

示例2

// xxx.ets
class MyDataSource implements IDataSource {
  private list: number[] = []

  constructor(list: number[]) {
    this.list = list
  }

  totalCount(): number {
    return this.list.length
  }

  getData(index: number): number {
    return this.list[index]
  }

  registerDataChangeListener(listener: DataChangeListener): void {
  }

  unregisterDataChangeListener() {
  }
}

@Entry
@Component
struct SwiperExample {
  private swiperController: SwiperController = new SwiperController()
  private data: MyDataSource = new MyDataSource([])

  aboutToAppear(): void {
    let list: number[] = []
    for (let i = 1; i <= 10; i++) {
      list.push(i);
    }
    this.data = new MyDataSource(list)
  }

  build() {
    Column({ space: 5 }) {
      Swiper(this.swiperController) {
        LazyForEach(this.data, (item: string) = > {
          Text(item.toString())
            .width('90%')
            .height(160)
            .backgroundColor(0xAFEEEE)
            .textAlign(TextAlign.Center)
            .fontSize(30)
        }, (item: string) = > item)
      }
      .cachedCount(2)
      .index(1)
      .autoPlay(true)
      .interval(4000)
      .indicator(Indicator.dot()
        .itemWidth(15)
        .itemHeight(15)
        .selectedItemWidth(15)
        .selectedItemHeight(15)
        .color(Color.Gray)
        .selectedColor(Color.Blue))
      .loop(true)
      .duration(1000)
      .itemSpace(0)
      .displayArrow(true, true)

      Row({ space: 12 }) {
        Button('showNext')
          .onClick(() = > {
            this.swiperController.showNext()
          })
        Button('showPrevious')
          .onClick(() = > {
            this.swiperController.showPrevious()
          })
      }.margin(5)
    }.width('100%')
    .margin({ top: 5 })
  }
}

swiper

示例3

搜狗高速浏览器截图20240326151547.png

// xxx.ets
class MyDataSource implements IDataSource {
  private list: number[] = []

  constructor(list: number[]) {
    this.list = list
  }

  totalCount(): number {
    return this.list.length
  }

  getData(index: number): number {
    return this.list[index]
  }

  registerDataChangeListener(listener: DataChangeListener): void {
  }

  unregisterDataChangeListener() {
  }
}

@Entry
@Component
struct SwiperExample {
  private swiperController: SwiperController = new SwiperController()
  private data: MyDataSource = new MyDataSource([])

  aboutToAppear(): void {
    let list: number[] = []
    for (let i = 1; i <= 10; i++) {
      list.push(i);
    }
    this.data = new MyDataSource(list)
  }

  build() {
    Column({ space: 5 }) {
      Swiper(this.swiperController) {
        LazyForEach(this.data, (item: string) = > {
          Text(item.toString())
            .width('90%')
            .height(160)
            .backgroundColor(0xAFEEEE)
            .textAlign(TextAlign.Center)
            .fontSize(30)
        }, (item: string) = > item)
      }
      .cachedCount(2)
      .index(1)
      .autoPlay(true)
      .interval(4000)
      .indicator(Indicator.digit()
        .right("43%")
        .top(200)
        .fontColor(Color.Gray)
        .selectedFontColor(Color.Gray)
        .digitFont({ size: 20, weight: FontWeight.Bold })
        .selectedDigitFont({ size: 20, weight: FontWeight.Normal }))
      .loop(true)
      .duration(1000)
      .itemSpace(0)
      .displayArrow(true, false)

      Row({ space: 12 }) {
        Button('showNext')
          .onClick(() = > {
            this.swiperController.showNext()
          })
        Button('showPrevious')
          .onClick(() = > {
            this.swiperController.showPrevious()
          })
      }.margin(5)
    }.width('100%')
    .margin({ top: 5 })
  }
}

swiper

审核编辑 黄宇

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 组件
    +关注

    关注

    1

    文章

    495

    浏览量

    17722
  • 鸿蒙
    +关注

    关注

    56

    文章

    2266

    浏览量

    42468
收藏 人收藏

    评论

    相关推荐

    HarmonyOS开发案例:【Swiper的实战】

    主要介绍了滑动容器组件Swiper的几种常见的应用场景,包括顶部导航、轮播图以及视频滑动播放。
    的头像 发表于 04-28 14:41 1065次阅读
    HarmonyOS开发案例:【<b class='flag-5'>Swiper</b>的实战】

    鸿蒙ArkTS容器组件:Column

    沿垂直方向布局的容器
    的头像 发表于 07-05 16:32 279次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>ArkTS</b><b class='flag-5'>容器</b><b class='flag-5'>组件</b>:Column

    鸿蒙ArkTS容器组件:Flex

    以弹性方式布局子组件容器组件
    的头像 发表于 07-08 10:19 260次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>ArkTS</b><b class='flag-5'>容器</b><b class='flag-5'>组件</b>:Flex

    鸿蒙ArkTS容器组件:FlowItem

    [瀑布流组件]的子组件,用来展示瀑布流具体item。
    的头像 发表于 07-08 09:56 216次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>ArkTS</b><b class='flag-5'>容器</b><b class='flag-5'>组件</b>:FlowItem

    鸿蒙ArkTS容器组件:GridCol

    栅格子组件,必须作为栅格容器组件([GridRow])的子组件使用。
    的头像 发表于 07-08 15:17 247次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>ArkTS</b><b class='flag-5'>容器</b><b class='flag-5'>组件</b>:GridCol

    鸿蒙ArkTS容器组件:GridItem

    网格容器中单项内容容器
    的头像 发表于 07-09 09:25 227次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>ArkTS</b><b class='flag-5'>容器</b><b class='flag-5'>组件</b>:GridItem

    鸿蒙ArkTS容器组件:ListItemGroup

    组件用来展示列表item分组,宽度默认充满[List]组件,必须配合List组件来使用。
    的头像 发表于 07-10 09:20 339次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>ArkTS</b><b class='flag-5'>容器</b><b class='flag-5'>组件</b>:ListItemGroup

    鸿蒙ArkTS容器组件:Navigator

    路由容器组件,提供路由跳转能力。
    的头像 发表于 07-10 14:55 269次阅读

    鸿蒙ArkTS容器组件:Refresh

    可以进行页面下拉操作并显示刷新动效的容器组件
    的头像 发表于 07-11 16:11 284次阅读

    鸿蒙ArkTS容器组件:RowSplit

    将子组件横向布局,并在每个子组件之间插入一根纵向的分割线。
    的头像 发表于 07-11 22:25 175次阅读

    鸿蒙ArkTS容器组件:Scroll

    可滚动的容器组件,当子组件的布局尺寸超过父组件的尺寸时,内容可以滚动。
    的头像 发表于 07-12 15:24 665次阅读

    鸿蒙ArkTS容器组件:SideBarContainer

    提供侧边栏可以显示和隐藏的侧边栏容器,通过子组件定义侧边栏和内容区,第一个子组件表示侧边栏,第二个子组件表示内容区。
    的头像 发表于 07-18 15:46 314次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>ArkTS</b><b class='flag-5'>容器</b><b class='flag-5'>组件</b>:SideBarContainer

    鸿蒙ArkTS容器组件:Stack

    堆叠容器,子组件按照顺序依次入栈,后一个子组件覆盖前一个子组件
    的头像 发表于 07-15 18:23 656次阅读

    鸿蒙ArkTS容器组件:Tabs

    通过页签进行内容视图切换的容器组件,每个页签对应一个内容视图。
    的头像 发表于 07-15 09:48 460次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>ArkTS</b><b class='flag-5'>容器</b><b class='flag-5'>组件</b>:Tabs

    鸿蒙ArkTS容器组件:WaterFlow

    瀑布流容器,由“行”和“列”分割的单元格所组成,通过容器自身的排列规则,将不同大小的“项目”自上而下,如瀑布般紧密布局。
    的头像 发表于 07-15 17:35 294次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>ArkTS</b><b class='flag-5'>容器</b><b class='flag-5'>组件</b>:WaterFlow