HarmonyOS 鸿蒙Next ArkTS语言教程入门学习第76天,创建轮播(Swiper)
HarmonyOS 鸿蒙Next ArkTS语言教程入门学习第76天,创建轮播(Swiper) 1、创建轮播(Swiper)
Swiper组件提供滑动轮播显示的能力。Swiper本身是一个容器组件,当设置了多个子组件后,可以对这些子组件进行轮播显示。通常,在一些应用首页显示推荐的内容时,需要用到轮播显示的能力。
1.1、布局与约束
Swiper作为一个容器组件,在自身尺寸属性未被设置时,会自动根据子组件的大小设置自身的尺寸。如果开发者对Swiper组件设置了固定的尺寸,则在轮播显示过程中均以该尺寸生效;否则,在轮播过程中,会根据子组件的大小自动调整自身的尺寸。
1.2、循环播放
通过loop属性控制是否循环播放,该属性默认值为true。
当loop为true时,在显示第一页或最后一页时,可以继续往前切换到前一页或者往后切换到后一页。如果loop为false,则在第一页或最后一页时,无法继续向前或者向后切换页面。
1.3、自动轮播
Swiper通过设置autoPlay属性,控制是否自动轮播子组件。该属性默认值为false。
autoPlay为true时,会自动切换播放子组件,子组件与子组件之间的播放间隔通过interval属性设置。interval属性默认值为3000,单位毫秒。
1.4、导航点样式
Swiper提供了默认的导航点样式,导航点默认显示在Swiper下方居中位置,开发者也可以通过indicatorStyle属性自定义导航点的位置和样式。
通过indicatorStyle属性,开发者可以设置导航点相对于Swiper组件上下左右四个方位的位置,同时也可以设置每个导航点的尺寸、颜色、蒙层和被选中导航点的颜色。
1.5、页面切换方式
Swiper支持三种页面切换方式:手指滑动、点击导航点和通过控制器。
通过控制器切换页面:
@Entry
@Component
struct SwiperDemo {
private swiperController: SwiperController = new SwiperController();
build() {
Column({ space: 5 }) {
Swiper(this.swiperController) {
Text("0")
.width(250)
.height(250)
.backgroundColor(Color.Gray)
.textAlign(TextAlign.Center)
.fontSize(30)
Text("1")
.width(250)
.height(250)
.backgroundColor(Color.Green)
.textAlign(TextAlign.Center)
.fontSize(30)
Text("2")
.width(250)
.height(250)
.backgroundColor(Color.Pink)
.textAlign(TextAlign.Center)
.fontSize(30)
}
.indicator(true)
Row({ space: 12 }) {
Button('showNext')
.onClick(() => {
this.swiperController.showNext(); // 通过controller切换到后一页
})
Button('showPrevious')
.onClick(() => {
this.swiperController.showPrevious(); // 通过controller切换到前一页
})
}.margin(5)
}.width('100%')
.margin({ top: 5 })
}
}
1.6、轮播方向
Swiper支持水平和垂直方向上进行轮播,主要通过vertical属性控制。
当vertical为true时,表示在垂直方向上进行轮播;为false时,表示在水平方向上进行轮播。vertical默认值为false。
设置水平方向上轮播:
Swiper(this.swiperController) {
...
}
.indicator(true)
.vertical(false)
设置垂直方向轮播:
Swiper(this.swiperController) {
...
}
.indicator(true)
.vertical(true)
1.7、每页显示多个子页面
Swiper支持在一个页面内同时显示多个子组件,通过displayCount属性设置。
设置一个页面内显示两个子组件:
Swiper(this.swiperController) {
Text("0")
.width(250)
.height(250)
.backgroundColor(Color.Gray)
.textAlign(TextAlign.Center)
.fontSize(30)
Text("1")
.width(250)
.height(250)
.backgroundColor(Color.Green)
.textAlign(TextAlign.Center)
.fontSize(30)
Text("2")
.width(250)
.height(250)
.backgroundColor(Color.Pink)
.textAlign(TextAlign.Center)
.fontSize(30)
Text("3")
.width(250)
.height(250)
.backgroundColor(Color.Blue)
.textAlign(TextAlign.Center)
.fontSize(30)
}
.indicator(true)
.displayCount(2)
更多关于HarmonyOS 鸿蒙Next ArkTS语言教程入门学习第76天,创建轮播(Swiper)的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,使用ArkTS语言创建轮播(Swiper)可以通过以下步骤实现:
-
导入相关模块:首先,导入
[@ohos](/user/ohos).arkui.advanced
模块中的Swiper
组件。import { Swiper } from '[@ohos](/user/ohos).arkui.advanced';
-
创建Swiper组件:在UI布局中创建
Swiper
组件,并设置其属性。[@Component](/user/Component) struct MySwiper { build() { Swiper({ loop: true, // 是否循环播放 autoPlay: true, // 是否自动播放 interval: 3000, // 自动播放间隔时间 indicator: true // 是否显示指示器 }) { // 添加子组件 Text('Page 1').fontSize(30); Text('Page 2').fontSize(30); Text('Page 3').fontSize(30); } .width('100%') .height(200); } }
-
设置Swiper属性:
Swiper
组件支持多种属性,如loop
(循环播放)、autoPlay
(自动播放)、interval
(播放间隔时间)、indicator
(显示指示器)等。 -
添加子组件:在
Swiper
组件中添加子组件,这些子组件将作为轮播的每一页内容。 -
调整布局:通过
width
和height
属性调整Swiper
组件的大小,以适应页面布局。
通过以上步骤,你可以在HarmonyOS鸿蒙Next中使用ArkTS语言创建一个简单的轮播组件。
更多关于HarmonyOS 鸿蒙Next ArkTS语言教程入门学习第76天,创建轮播(Swiper)的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS的ArkTS语言中,创建轮播(Swiper)组件可以通过以下步骤实现:
- 引入Swiper组件:在页面中引入
Swiper
组件,用于展示轮播内容。 - 配置Swiper属性:设置
Swiper
的属性,如autoplay
(自动播放)、interval
(播放间隔)、indicator
(指示器)等。 - 添加子组件:在
Swiper
中添加子组件,如Image
或Text
,用于展示轮播的每一项内容。 - 样式调整:根据需要调整
Swiper
及其子组件的样式,确保轮播效果符合设计需求。
示例代码:
@Entry
@Component
struct SwiperExample {
build() {
Swiper() {
Image($r('app.media.image1')).width('100%').height('100%')
Image($r('app.media.image2')).width('100%').height('100%')
Image($r('app.media.image3')).width('100%').height('100%')
}
.autoplay(true)
.interval(3000)
.indicator(true)
}
}