Swiper
是页面切换类容器组件,它提供了切换页面显示的能力, Swiper
包含的每一个子组件就表示一个页面,例如 Swiper
有 4 个子组件,那么 Swiper
就有 4 个页面。 Swiper
可以绑定一个 SwiperController
控制显示上一页或者下一页。
interface SwiperInterface {
(controller?: SwiperController): SwiperAttribute;
}
Swiper
绑定一个控制器,控制页面翻页。简单样例如下:
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)
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;
}
简单样例如下所示:
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) // 设置关闭循环翻页,当显示最后一页后首页时无法继续往下翻页
declare class SwiperAttribute<T> extends CommonMethod<T> {
onChange(event: (index: number) => void): T;
}
SwiperController
是 Swiper
的页面切换控制器,可以将此对象绑定至 Swiper
组件上,然后通过它控制翻页, SwiperController
定义如下:
export declare class SwiperController {
showNext();
showPrevious();
}
简单样例如下所示:
@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 })
}
}
如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。