文档管理中心
开发与测试开放能力API应用框架ArkUI(方舟UI框架)UI开发 (ArkTS声明式开发范式)媒体展示创建轮播 (Swiper)

创建轮播 (Swiper)

Swiper组件提供滑动轮播显示的能力。Swiper本身是一个容器组件,当设置了多个子组件后,可以对这些子组件进行轮播显示。通常,在一些应用首页显示推荐的内容时,需要用到轮播显示的能力。

针对复杂页面场景,可以使用Swiper组件的预加载机制,利用主线程的空闲时间来提前构建和布局绘制组件,优化滑动体验。

布局与约束

Swiper作为一个容器组件,如果设置了自身尺寸属性,则在轮播显示过程中均以该尺寸生效。如果自身尺寸属性未被设置,则分两种情况:如果设置了prevMargin或者nextMargin属性,则Swiper自身尺寸会跟随其父组件;如果未设置prevMargin或者nextMargin属性,则会自动根据子组件的大小设置自身的尺寸。

循环播放

通过loop属性控制是否循环播放,该属性默认值为true。

当loop为true时,在显示第一页或最后一页时,可以继续往前切换到前一页或者往后切换到后一页。如果loop为false,则在第一页或最后一页时,无法继续向前或者向后切换页面。

  • loop为true
收起
自动换行
深色代码主题
复制
  1. Swiper() {
  2. Text('0')
  3. .width('90%')
  4. .height('100%')
  5. .backgroundColor(Color.Gray)
  6. .textAlign(TextAlign.Center)
  7. .fontSize(30)
  8. Text('1')
  9. .width('90%')
  10. .height('100%')
  11. .backgroundColor(Color.Green)
  12. .textAlign(TextAlign.Center)
  13. .fontSize(30)
  14. Text('2')
  15. .width('90%')
  16. .height('100%')
  17. .backgroundColor(Color.Pink)
  18. .textAlign(TextAlign.Center)
  19. .fontSize(30)
  20. }
  21. // ...
  22. .loop(true)

  • loop为false
收起
自动换行
深色代码主题
复制
  1. Swiper() {
  2. // ···
  3. }
  4. // ···
  5. .loop(false)

自动轮播

Swiper通过设置autoPlay属性,控制是否自动轮播子组件。该属性默认值为false。

autoPlay为true时,会自动切换播放子组件,子组件与子组件之间的播放间隔通过interval属性设置。interval属性默认值为3000,单位毫秒。

收起
自动换行
深色代码主题
复制
  1. Swiper() {
  2. // ···
  3. }
  4. // ···
  5. .loop(true)
  6. .autoPlay(true)
  7. .interval(1000)

导航点样式

Swiper提供了默认的导航点样式和导航点箭头样式,导航点默认显示在Swiper下方居中位置,开发者也可以通过indicator属性自定义导航点的位置和样式,导航点箭头默认不显示。

通过indicator属性,开发者可以设置导航点相对于Swiper组件上下左右四个方位的位置,同时也可以设置每个导航点的尺寸、颜色、蒙层和被选中导航点的颜色。

  • 导航点使用默认样式
收起
自动换行
深色代码主题
复制
  1. Swiper() {
  2. Text('0')
  3. .width('90%')
  4. .height('100%')
  5. .backgroundColor(Color.Gray)
  6. .textAlign(TextAlign.Center)
  7. .fontSize(30)
  8. Text('1')
  9. .width('90%')
  10. .height('100%')
  11. .backgroundColor(Color.Green)
  12. .textAlign(TextAlign.Center)
  13. .fontSize(30)
  14. Text('2')
  15. .width('90%')
  16. .height('100%')
  17. .backgroundColor(Color.Pink)
  18. .textAlign(TextAlign.Center)
  19. .fontSize(30)
  20. }

  • 自定义导航点样式

选中的导航点,宽度设为30vp且高度设为15vp,且颜色为蓝色;未选中的导航点,直径设为15vp,颜色设为红色。

收起
自动换行
深色代码主题
复制
  1. Swiper() {
  2. // ···
  3. }
  4. // ···
  5. .indicator(
  6. Indicator.dot()
  7. .left(0)
  8. .itemWidth(15)
  9. .itemHeight(15)
  10. .selectedItemWidth(30)
  11. .selectedItemHeight(15)
  12. .color(Color.Red)
  13. .selectedColor(Color.Blue)
  14. )

Swiper通过设置displayArrow属性,可以控制导航点箭头的大小、位置、颜色,底板的大小及颜色,以及鼠标悬停时是否显示箭头。

  • 箭头使用默认样式
收起
自动换行
深色代码主题
复制
  1. Swiper() {
  2. // ···
  3. }
  4. // ···
  5. .displayArrow(true, false)

  • 自定义箭头样式

箭头显示在组件两侧,大小为18vp,导航点箭头颜色设为蓝色。

收起
自动换行
深色代码主题
复制
  1. Swiper() {
  2. // ···
  3. }
  4. // ···
  5. .displayArrow({
  6. showBackground: true,
  7. isSidebarMiddle: true,
  8. backgroundSize: 24,
  9. backgroundColor: Color.White,
  10. arrowSize: 18,
  11. arrowColor: Color.Blue
  12. }, false)

页面切换方式

Swiper支持手指滑动、点击导航点和通过控制器三种方式切换页面,以下示例展示通过控制器切换页面的方法。

收起
自动换行
深色代码主题
复制
  1. // 如需作为页面入口,请取消@Entry的注释并删除export关键字
  2. // @Entry
  3. @Component
  4. export struct SwiperPageSwitchMethod {
  5. private swiperBackgroundColors: Color[] = [Color.Blue, Color.Gray, Color.Green, Color.Orange, Color.Brown,
  6. Color.Pink, Color.Red, Color.Yellow];
  7. private swiperAnimationMode: (SwiperAnimationMode | boolean | undefined)[] = [undefined, true, false,
  8. SwiperAnimationMode.NO_ANIMATION, SwiperAnimationMode.DEFAULT_ANIMATION, SwiperAnimationMode.FAST_ANIMATION];
  9. private swiperController: SwiperController = new SwiperController();
  10. private animationModeIndex: number = 0;
  11. private animationMode: (SwiperAnimationMode | boolean | undefined) = undefined;
  12. @State animationModeStr: string = 'undefined';
  13. @State targetIndex: number = 0;
  14. aboutToAppear(): void {
  15. this.toSwiperAnimationModeStr();
  16. }
  17. build() {
  18. // ...
  19. Column({ space: 5 }) {
  20. Swiper(this.swiperController) {
  21. ForEach(this.swiperBackgroundColors, (backgroundColor: Color, index: number) => {
  22. Text(index.toString())
  23. .width(250)
  24. .height(250)
  25. .backgroundColor(backgroundColor)
  26. .textAlign(TextAlign.Center)
  27. .fontSize(30)
  28. })
  29. }
  30. // ...
  31. .indicator(true)
  32. Row({ space: 12 }) {
  33. Button('showNext')
  34. .onClick(() => {
  35. this.swiperController.showNext(); // 通过controller切换到后一页
  36. })
  37. Button('showPrevious')
  38. .onClick(() => {
  39. this.swiperController.showPrevious(); // 通过controller切换到前一页
  40. })
  41. }.margin(5)
  42. Row({ space: 12 }) {
  43. Text('Index:')
  44. Button(this.targetIndex.toString())
  45. .onClick(() => {
  46. this.targetIndex = (this.targetIndex + 1) % this.swiperBackgroundColors.length;
  47. })
  48. }.margin(5)
  49. Row({ space: 12 }) {
  50. Text('AnimationMode:')
  51. Button(this.animationModeStr)
  52. .onClick(() => {
  53. this.animationModeIndex = (this.animationModeIndex + 1) % this.swiperAnimationMode.length;
  54. this.toSwiperAnimationModeStr();
  55. })
  56. }.margin(5)
  57. Row({ space: 12 }) {
  58. Button('changeIndex(' + this.targetIndex + ', ' + this.animationModeStr + ')')
  59. .onClick(() => {
  60. this.swiperController.changeIndex(this.targetIndex, this.animationMode); // 通过controller切换到指定页
  61. })
  62. }.margin(5)
  63. }
  64. // ...
  65. }
  66. private toSwiperAnimationModeStr() {
  67. this.animationMode = this.swiperAnimationMode[this.animationModeIndex];
  68. if ((this.animationMode === true) || (this.animationMode === false)) {
  69. this.animationModeStr = '' + this.animationMode;
  70. } else if ((this.animationMode === SwiperAnimationMode.NO_ANIMATION) ||
  71. (this.animationMode === SwiperAnimationMode.DEFAULT_ANIMATION) ||
  72. (this.animationMode === SwiperAnimationMode.FAST_ANIMATION)) {
  73. this.animationModeStr = SwiperAnimationMode[this.animationMode];
  74. } else {
  75. this.animationModeStr = 'undefined';
  76. }
  77. }
  78. }

轮播方向

Swiper支持水平和垂直方向上进行轮播,主要通过vertical属性控制。

当vertical为true时,表示在垂直方向上进行轮播;为false时,表示在水平方向上进行轮播。vertical默认值为false。

  • 设置水平方向上轮播。
收起
自动换行
深色代码主题
复制
  1. Swiper(
  2. // ···
  3. ) {
  4. // ···
  5. }
  6. // ···
  7. .indicator(true)
  8. .vertical(false)

  • 设置垂直方向轮播。
收起
自动换行
深色代码主题
复制
  1. Swiper(
  2. // ···
  3. ) {
  4. // ···
  5. }
  6. // ···
  7. .indicator(true)
  8. .vertical(true)

每页显示多个子页面

Swiper支持在一个页面内同时显示多个子组件,通过displayCount属性设置。

收起
自动换行
深色代码主题
复制
  1. Swiper() {
  2. Text('0')
  3. .width(250)
  4. .height(250)
  5. .backgroundColor(Color.Gray)
  6. .textAlign(TextAlign.Center)
  7. .fontSize(30)
  8. Text('1')
  9. .width(250)
  10. .height(250)
  11. .backgroundColor(Color.Green)
  12. .textAlign(TextAlign.Center)
  13. .fontSize(30)
  14. Text('2')
  15. .width(250)
  16. .height(250)
  17. .backgroundColor(Color.Pink)
  18. .textAlign(TextAlign.Center)
  19. .fontSize(30)
  20. Text('3')
  21. .width(250)
  22. .height(250)
  23. .backgroundColor(Color.Yellow)
  24. .textAlign(TextAlign.Center)
  25. .fontSize(30)
  26. }
  27. // ...
  28. .indicator(true)
  29. .displayCount(2)

自定义切换动画

Swiper支持通过customContentTransition设置自定义切换动画,可以在回调中对视窗内所有页面逐帧设置透明度、缩放比例、位移、渲染层级等属性实现自定义切换动画。

收起
自动换行
深色代码主题
复制
  1. // 如需作为页面入口,请取消@Entry的注释并删除export关键字
  2. // @Entry
  3. @Component
  4. export struct SwiperCustomAnimation {
  5. private DISPLAY_COUNT: number = 2;
  6. private MIN_SCALE: number = 0.75;
  7. @State backgroundColors: Color[] = [Color.Green, Color.Blue, Color.Yellow, Color.Pink, Color.Gray, Color.Orange];
  8. @State opacityList: number[] = [];
  9. @State scaleList: number[] = [];
  10. @State translateList: number[] = [];
  11. @State zIndexList: number[] = [];
  12. aboutToAppear(): void {
  13. for (let i = 0; i < this.backgroundColors.length; i++) {
  14. this.opacityList.push(1.0);
  15. this.scaleList.push(1.0);
  16. this.translateList.push(0.0);
  17. this.zIndexList.push(0);
  18. }
  19. }
  20. build() {
  21. // ...
  22. Column({ space: 12 }) {
  23. // ...
  24. Swiper() {
  25. ForEach(this.backgroundColors, (backgroundColor: Color, index: number) => {
  26. Text(index.toString())
  27. .width('100%')
  28. .height('100%')
  29. .fontSize(50)
  30. .textAlign(TextAlign.Center)
  31. .backgroundColor(backgroundColor)
  32. .opacity(this.opacityList[index])
  33. .scale({ x: this.scaleList[index], y: this.scaleList[index] })
  34. .translate({ x: this.translateList[index] })
  35. .zIndex(this.zIndexList[index])
  36. })
  37. }
  38. .height(300)
  39. .indicator(false)
  40. .displayCount(this.DISPLAY_COUNT, true)
  41. .customContentTransition({
  42. timeout: 1000,
  43. transition: (proxy: SwiperContentTransitionProxy) => {
  44. if (proxy.position <= proxy.index % this.DISPLAY_COUNT ||
  45. proxy.position >= this.DISPLAY_COUNT + proxy.index % this.DISPLAY_COUNT) {
  46. // 同组页面完全滑出视窗外时,重置属性值
  47. this.opacityList[proxy.index] = 1.0;
  48. this.scaleList[proxy.index] = 1.0;
  49. this.translateList[proxy.index] = 0.0;
  50. this.zIndexList[proxy.index] = 0;
  51. } else {
  52. // 同组页面未滑出视窗外时,对同组中左右两个页面,逐帧根据position修改属性值
  53. if (proxy.index % this.DISPLAY_COUNT === 0) {
  54. this.opacityList[proxy.index] = 1 - proxy.position / this.DISPLAY_COUNT;
  55. this.scaleList[proxy.index] =
  56. this.MIN_SCALE + (1 - this.MIN_SCALE) * (1 - proxy.position / this.DISPLAY_COUNT);
  57. this.translateList[proxy.index] = -proxy.position * proxy.mainAxisLength +
  58. (1 - this.scaleList[proxy.index]) * proxy.mainAxisLength / 2.0;
  59. } else {
  60. this.opacityList[proxy.index] = 1 - (proxy.position - 1) / this.DISPLAY_COUNT;
  61. this.scaleList[proxy.index] =
  62. this.MIN_SCALE + (1 - this.MIN_SCALE) * (1 - (proxy.position - 1) / this.DISPLAY_COUNT);
  63. this.translateList[proxy.index] = -(proxy.position - 1) * proxy.mainAxisLength -
  64. (1 - this.scaleList[proxy.index]) * proxy.mainAxisLength / 2.0;
  65. }
  66. this.zIndexList[proxy.index] = -1;
  67. }
  68. }
  69. })
  70. // ...
  71. }
  72. .width('100%')
  73. // ...
  74. }
  75. }

Swiper与Tabs联动

从API version 18开始,Swiper选中的元素改变时,会通过onSelected回调事件,将元素的索引值index返回。通过调用tabsController.changeIndex(index)方法来实现Tabs页签的切换。

收起
自动换行
深色代码主题
复制
  1. // xxx.ets
  2. class MyDataSource implements IDataSource {
  3. private list: number[] = [];
  4. constructor(list: number[]) {
  5. this.list = list;
  6. }
  7. totalCount(): number {
  8. return this.list.length;
  9. }
  10. getData(index: number): number {
  11. return this.list[index];
  12. }
  13. registerDataChangeListener(listener: DataChangeListener): void {
  14. }
  15. unregisterDataChangeListener() {
  16. }
  17. }
  18. // 如需作为页面入口,请取消@Entry的注释并删除export关键字
  19. // @Entry
  20. @Component
  21. export struct SwiperAndTabsLinkage {
  22. @State fontColor: string = '#182431';
  23. @State selectedFontColor: string = '#007DFF';
  24. @State currentIndex: number = 0;
  25. private list: number[] = [];
  26. private tabsController: TabsController = new TabsController();
  27. private swiperController: SwiperController = new SwiperController();
  28. private swiperData: MyDataSource = new MyDataSource([]);
  29. private context = this.getUIContext().getHostContext() as common.UIAbilityContext;
  30. aboutToAppear(): void {
  31. for (let i = 0; i <= 9; i++) {
  32. this.list.push(i);
  33. }
  34. this.swiperData = new MyDataSource(this.list);
  35. }
  36. @Builder tabBuilder(index: number, name: string) {
  37. Column() {
  38. Text(name)
  39. .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)
  40. .fontSize(16)
  41. .fontWeight(this.currentIndex === index ? 500 : 400)
  42. .lineHeight(22)
  43. .margin({ top: 17, bottom: 7 })
  44. Divider()
  45. .strokeWidth(2)
  46. .color('#007DFF')
  47. .opacity(this.currentIndex === index ? 1 : 0)
  48. }.width('20%')
  49. }
  50. build() {
  51. // ...
  52. Column() {
  53. Tabs({ barPosition: BarPosition.Start, controller: this.tabsController }) {
  54. ForEach(this.list, (index: number) =>{
  55. // 请在resources\base\element\string.json文件中配置name为'swiper_text1' ,value为非空字符串的资源
  56. TabContent().tabBar(this.tabBuilder(index,
  57. this.context.resourceManager.getStringByNameSync('swiper_text1') + this.list[index]))
  58. })
  59. }
  60. .onTabBarClick((index: number) => {
  61. this.currentIndex = index;
  62. this.swiperController.changeIndex(index, true);
  63. })
  64. .barMode(BarMode.Scrollable)
  65. .backgroundColor('#F1F3F5')
  66. .height(56)
  67. .width('100%')
  68. Swiper(this.swiperController) {
  69. LazyForEach(this.swiperData, (item: number) => {
  70. Text(item.toString())
  71. .onAppear(()=>{
  72. console.info('onAppear ' + item.toString());
  73. })
  74. .onDisAppear(()=>{
  75. console.info('onDisAppear ' + item.toString());
  76. })
  77. .width('100%')
  78. .height('40%')
  79. .backgroundColor(0xAFEEEE)
  80. .textAlign(TextAlign.Center)
  81. .fontSize(30)
  82. }, (item: number) => item.toString())
  83. }
  84. .loop(false)
  85. .onSelected((index: number) => {
  86. console.info('onSelected:' + index);
  87. this.currentIndex = index;
  88. this.tabsController.changeIndex(index);
  89. })
  90. }
  91. // ...
  92. }
  93. }

设置圆点导航点间距

从API version 19开始,针对圆点导航点,可以通过DotIndicator的space属性来设置圆点导航点的间距。

收起
自动换行
深色代码主题
复制
  1. Swiper(
  2. // ···
  3. ) {
  4. // ···
  5. }
  6. .indicator(new DotIndicator()
  7. .space(this.space)
  8. // ···
  9. )

导航点忽略组件大小

当导航点的bottom设为0之后,导航点的底部与Swiper的底部还会有一定间距。如果希望消除该间距,从API version 19开始,可通过调用bottom(bottom, ignoreSize)属性来进行设置。将ignoreSize设置为true,即可忽略导航点组件大小,达到消除该间距的目的。

  • 圆点导航点忽略组件大小。
收起
自动换行
深色代码主题
复制
  1. Swiper(
  2. // ···
  3. ) {
  4. // ···
  5. }
  6. .indicator(new DotIndicator()
  7. // ···
  8. .bottom(LengthMetrics.vp(0), this.ignoreSize) // true
  9. // ···
  10. )
  • 数字导航点忽略组件大小。
收起
自动换行
深色代码主题
复制
  1. Swiper(
  2. // ···
  3. ) {
  4. // ···
  5. }
  6. .indicator(new DigitIndicator()
  7. .bottom(LengthMetrics.vp(0), true)
  8. )

圆点导航点设置间距及忽略组件大小完整示例代码如下:

收起
自动换行
深色代码主题
复制
  1. import { LengthMetrics } from '@kit.ArkUI';
  2. // ...
  3. class MyDataSource implements IDataSource {
  4. private list: number[] = [];
  5. constructor(list: number[]) {
  6. this.list = list;
  7. }
  8. totalCount(): number {
  9. return this.list.length;
  10. }
  11. getData(index: number): number {
  12. return this.list[index];
  13. }
  14. registerDataChangeListener(listener: DataChangeListener): void {
  15. }
  16. unregisterDataChangeListener() {
  17. }
  18. }
  19. // 如需作为页面入口,请取消@Entry的注释并删除export关键字
  20. // @Entry
  21. @Component
  22. export struct SwiperIgnoreComponentSize {
  23. @State space: LengthMetrics = LengthMetrics.vp(0);
  24. @State spacePool: LengthMetrics[] = [LengthMetrics.vp(0), LengthMetrics.px(3), LengthMetrics.vp(10)];
  25. @State spaceIndex: number = 0;
  26. @State ignoreSize: boolean = false;
  27. @State ignoreSizePool: boolean[] = [false, true];
  28. @State ignoreSizeIndex: number = 0;
  29. private swiperController1: SwiperController = new SwiperController();
  30. private data1: MyDataSource = new MyDataSource([]);
  31. aboutToAppear(): void {
  32. let list1: number[] = [];
  33. for (let i = 1; i <= 10; i++) {
  34. list1.push(i);
  35. }
  36. this.data1 = new MyDataSource(list1);
  37. }
  38. build() {
  39. // ...
  40. Scroll() {
  41. Column({ space: 20 }) {
  42. Swiper(
  43. this.swiperController1
  44. ) {
  45. LazyForEach(this.data1, (item: number) => {
  46. Text(item.toString())
  47. .width('90%')
  48. .height(120)
  49. .backgroundColor(0xAFEEEE)
  50. .textAlign(TextAlign.Center)
  51. .fontSize(30)
  52. }, (item: number) => item.toString())
  53. }
  54. .indicator(new DotIndicator()
  55. .space(this.space)
  56. .bottom(LengthMetrics.vp(0), this.ignoreSize) // true
  57. .itemWidth(15)
  58. .itemHeight(15)
  59. .selectedItemWidth(15)
  60. .selectedItemHeight(15)
  61. .color(Color.Gray)
  62. .selectedColor(Color.Blue)
  63. )
  64. .displayArrow({
  65. showBackground: true,
  66. isSidebarMiddle: true,
  67. backgroundSize: 24,
  68. backgroundColor: Color.White,
  69. arrowSize: 18,
  70. arrowColor: Color.Blue
  71. }, false)
  72. Column({ space: 4 }) {
  73. Button('spaceIndex:' + this.spaceIndex).onClick(() => {
  74. this.spaceIndex = (this.spaceIndex + 1) % this.spacePool.length;
  75. this.space = this.spacePool[this.spaceIndex];
  76. }).margin(10)
  77. Button('ignoreSizeIndex:' + this.ignoreSizeIndex).onClick(() => {
  78. this.ignoreSizeIndex = (this.ignoreSizeIndex + 1) % this.ignoreSizePool.length;
  79. this.ignoreSize = this.ignoreSizePool[this.ignoreSizeIndex];
  80. }).margin(10)
  81. }.margin(2)
  82. }.width('100%')
  83. }
  84. // ...
  85. }
  86. }

保持可见内容位置不变

从API version 20开始,Swiper通过设置maintainVisibleContentPosition属性,可在使用LazyForEach懒加载数据时(如通过onDataAdd新增数据),保持当前可见内容位置不变,避免因数据增删导致的视图跳动。该属性默认值为false。

maintainVisibleContentPosition为true时,显示区域上方或前方插入或删除数据时可见内容位置不变。

关于数据LazyForEach:数据懒加载的具体使用,可参考数据懒加载章节中的示例。

收起
自动换行
深色代码主题
复制
  1. // xxx.ets
  2. class MyDataSource implements IDataSource {
  3. private listeners: DataChangeListener[] = [];
  4. private dataArray: string[] = ['0', '1', '2', '3', '4', '5', '6'];
  5. public totalCount(): number {
  6. return this.dataArray.length;
  7. }
  8. public getData(index: number): string | undefined {
  9. return this.dataArray[index];
  10. }
  11. public addData(index: number, data: string): void {
  12. this.dataArray.splice(index, 0, data);
  13. this.listeners.forEach(listener => {
  14. listener.onDataAdd(index);
  15. })
  16. }
  17. public deleteData(index: number): void {
  18. this.dataArray.splice(index, 1);
  19. this.listeners.forEach(listener => {
  20. listener.onDataDelete(index);
  21. })
  22. }
  23. registerDataChangeListener(listener: DataChangeListener): void {
  24. if (this.listeners.indexOf(listener) < 0) {
  25. hilog.info(DOMAIN, 'testTag', 'add listener');
  26. this.listeners.push(listener);
  27. }
  28. }
  29. unregisterDataChangeListener(listener: DataChangeListener): void {
  30. const pos = this.listeners.indexOf(listener);
  31. if (pos >= 0) {
  32. hilog.info(DOMAIN, 'testTag', 'remove listener');
  33. this.listeners.splice(pos, 1);
  34. }
  35. }
  36. }
  37. // 如需作为页面入口,请取消@Entry的注释并删除export关键字
  38. // @Entry
  39. @Component
  40. export struct SwiperVisibleContentPosition {
  41. private data: MyDataSource = new MyDataSource();
  42. @State index: number = 3;
  43. build() {
  44. // ...
  45. Column({ space: 12 }) {
  46. // ...
  47. Swiper() {
  48. LazyForEach(this.data, (item: string) => {
  49. Text(item.toString())
  50. .width('90%')
  51. .height(160)
  52. .backgroundColor(0xAFEEEE)
  53. .textAlign(TextAlign.Center)
  54. .fontSize(30)
  55. })
  56. }
  57. .onChange((index) => {
  58. this.index = index;
  59. })
  60. .index(3)
  61. .maintainVisibleContentPosition(true)
  62. // ...
  63. Column({ space: 12 }) {
  64. Text('index:' + this.index).fontSize(20)
  65. Row() {
  66. // 在LazyForEach索引为0的位置添加数据
  67. Button('header data add').height(30).onClick(() => {
  68. this.data.addData(0, 'header Data');
  69. })
  70. // 删除LazyForEach索引为0的位置数据
  71. Button('header data delete').height(30).onClick(() => {
  72. this.data.deleteData(0);
  73. })
  74. }
  75. }.margin(5)
  76. // ...
  77. }.width('100%')
  78. .margin({ top: 5 })
  79. // ...
  80. }
  81. }

示例代码

在 开发与测试 开放能力API 中进行搜索
请输入您想要搜索的关键词