Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Swiper Component in HarmonyOS ArkUI

Tech May 8 3

4. Auto Play

When loop is set to true, you can continue swiping forward or backward even on the first or last page. If loop is false, swiping past the first or last page is disabled.

@Entry
@Component
struct Index {
  private controller: SwiperController = new SwiperController()
  build() {
    Swiper(this.controller) {
      Text("0")
        .width('100%')
        .height('100%')
        .backgroundColor(Color.Gray)
        .textAlign(TextAlign.Center)
        .fontSize(30)

      Text("1")
        .width('100%')
        .height('100%')
        .backgroundColor(Color.Green)
        .textAlign(TextAlign.Center)
        .fontSize(30)

      Text("2")
        .width('100%')
        .height('100%')
        .backgroundColor(Color.Pink)
        .textAlign(TextAlign.Center)
        .fontSize(30)
    }
    .loop(true)
    .autoPlay(true)
    .interval(2000)
  }
}

Auto play demo

When autoPlay is true, the Swiper automatically switches between child components. The interval between switches is set by the interval property, which defaults to 2000 milliseconds.

5. Indicator Style

@Entry
@Component
struct Index {
  private controller: SwiperController = new SwiperController()
  build() {
    Swiper(this.controller) {
      Text("0")
        .width('100%')
        .height('100%')
        .backgroundColor(Color.Gray)
        .textAlign(TextAlign.Center)
        .fontSize(30)

      Text("1")
        .width('100%')
        .height('100%')
        .backgroundColor(Color.Green)
        .textAlign(TextAlign.Center)
        .fontSize(30)

      Text("2")
        .width('100%')
        .height('100%')
        .backgroundColor(Color.Pink)
        .textAlign(TextAlign.Center)
        .fontSize(30)
    }
    .indicatorStyle({
      size: 30,
      left: 0,
      color: Color.Red
    })
  }
}

Indicator style demo

The indicatorStyle property allows you to set the position of the navigation dots reltaive too the Swiper component (top, bottom, left, right), as well as the size, color, and colors for selected and unselected dots.

6. Page Switching Methods

Swiper supports three page switching methods: finger swiping, tapping navigation dots, and using a controller.

@Entry
@Component
struct Index {
  private controller: SwiperController = new SwiperController();

  build() {
    Column({ space: 5 }) {
      Swiper(this.controller) {
        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('Next')
          .onClick(() => {
            this.controller.showNext();
          })
        Button('Previous')
          .onClick(() => {
            this.controller.showPrevious();
          })
      }.margin(5)
    }.width('100%')
    .margin({ top: 5 })
  }
}

Page switching demo

7. Swipe Direction

Set vertical to true for vertical swiping, or false for horizontal. The default is false.

@Entry
@Component
struct Index {
  private controller: SwiperController = new SwiperController();

  build() {
    Column({ space: 5 }) {
      Swiper(this.controller) {
        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).vertical(false)

      Swiper(this.controller) {
        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).vertical(true)
    }
  }
}

Swipe direction demo

8. Display Multiple Items Per Page

Swiper can display multiple child components on the same page by setting the displayCount property.

@Entry
@Component
struct Index {
  private controller: SwiperController = new SwiperController();

  build() {
    Swiper(this.controller) {
      Text("0")
        .width(250)
        .height(250)
        .backgroundColor(Color.Gray)
        .textAlign(TextAlign.Center)
        .fontSize(30)
      // Additional items can be added here
    }
    .displayCount(2) // Show two items per page
  }
}

Note: The original code snippet for this section was incomplete; the above is a corrected example.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.