前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >ArkUI滚动类组件-Swiper

ArkUI滚动类组件-Swiper

原创
作者头像
小帅聊鸿蒙
发布2024-10-07 14:25:03
发布2024-10-07 14:25:03
1540
举报
文章被收录于专栏:鸿蒙开发笔记鸿蒙开发笔记

Swiper 是页面切换类容器组件,它提供了切换页面显示的能力, Swiper 包含的每一个子组件就表示一个页面,例如 Swiper 有 4 个子组件,那么 Swiper 就有 4 个页面。 Swiper 可以绑定一个 SwiperController 控制显示上一页或者下一页。

Swiper定义介绍

代码语言:ts
复制
interface SwiperInterface {
  (controller?: SwiperController): SwiperAttribute;
}
  • controller:给 Swiper 绑定一个控制器,控制页面翻页。

简单样例如下:

代码语言:ts
复制
Swiper() {
  Text('Page1')
    .fontSize(20)
    .backgroundColor('#aabbcc')
  Text('Page2')
    .fontSize(20)
    .backgroundColor('#aabbcc')
  Text('Page3')
    .fontSize(20)
    .backgroundColor('#aabbcc')
}
.width('90%')
.height(120)
.backgroundColor(Color.Pink)

Swiper属性介绍

代码语言:ts
复制
declare class SwiperAttribute<T> extends CommonMethod<T> {
  index(value: number): T;
  autoPlay(value: boolean): T;
  interval(value: number): T;
  indicator(value: boolean): T;
  loop(value: boolean): T;
  duration(value: number): T;
  vertical(value: boolean): T;
  itemSpace(value: number | string): T;
  displayMode(value: SwiperDisplayMode): T;
  onChange(event: (index: number) => void): T;
}
  • index:默认显示显示第几页,默认值为 0。
  • autoPlay:是否自动播放,默认值为 false ,当设置为自动播放时,导航点无法点击。
  • interval:设置自动播放时,播放的时间间隔,单位毫秒,默认是 3000。
  • indicator:是否显示导航点指示器,默认显示。
  • loop:是否开启循环显示,也就是说当翻页到最后一页再往下翻页是否会回到第一页,默认开启。
  • duration:页面切换的动画时长,单位为毫秒,默认是 400。
  • vertical:是否竖直翻页,默认是 false。

简单样例如下所示:

代码语言:ts
复制
Swiper() {
  Text('Page1')
    .fontSize(20)
    .backgroundColor('#aabbcc')
  Text('Page2')
    .fontSize(20)
    .backgroundColor('#aabbcc')
  Text('Page3')
    .fontSize(20)
    .backgroundColor('#aabbcc')
}
.width('90%')
.height(120)
.backgroundColor(Color.Pink)
.index(1)                     // 设置默认显示第二页
.indicator(true)              // 设置显示导航指示器
.vertical(true)               // 设置竖直翻页
.loop(false)                  // 设置关闭循环翻页,当显示最后一页后首页时无法继续往下翻页

Swiper事件介绍

代码语言:ts
复制
declare class SwiperAttribute<T> extends CommonMethod<T> {
  onChange(event: (index: number) => void): T;
}
  • onChange:页面切换时回到当前方法,显示当前第几页。

SwiperController简介

SwiperController 是 Swiper 的页面切换控制器,可以将此对象绑定至 Swiper 组件上,然后通过它控制翻页, SwiperController 定义如下:

代码语言:ts
复制
export declare class SwiperController {
  showNext();
  showPrevious();
}
  • showNext:显示下一页。
  • showPrevious:显示上一页。

简单样例如下所示:

代码语言:ts
复制
@Entry @Component struct ComponentTest {

  private controller: SwiperController = new SwiperController();

  build() {
    Column() {
      Swiper(this.controller) {      // 绑定翻页控制器
        Text('Page1')
          .fontSize(20)
          .backgroundColor('#aabbcc')
        Text('Page2')
          .fontSize(20)
          .backgroundColor('#aabbcc')
        Text('Page3')
          .fontSize(20)
          .backgroundColor('#aabbcc')
      }
      .width('90%')
      .height(120)
      .backgroundColor(Color.Pink)
      .index(1)                      // 默认显示第二页
      .indicator(true)               // 显示导航指示器

      Row({space: 20}) {
        Button('上一页')
          .onClick(() => {
            this.controller.showPrevious();
          })
        Button('下一页')
          .onClick(() => {
            this.controller.showNext();
          })
      }
      .margin({top: 10})
    }
    .alignItems(HorizontalAlign.Center)
    .width('100%')
    .height('100%')
    .padding({ left: 20, right: 20, top: 10 })
  }
}

写在最后

如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:

  • 点赞,转发,有你们的 『点赞和评论』,才是我创造的动力;
  • 关注小编,同时可以期待后续文章ing🚀,不定期分享原创知识;
  • 想要获取更多完整鸿蒙最新学习知识点,可关注B站:码牛课堂鸿蒙开发;

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Swiper定义介绍
  • Swiper属性介绍
  • Swiper事件介绍
  • SwiperController简介
  • 写在最后
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档