HarmonyOS 多图片合集轮播+进度条

多图片合集使用swiper轮播,指示器按照图片的停留时间下方为对应的进度条。

HarmonyOS
2024-12-26 14:45:59
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

请参考下方代码:

class MyDataSource implements IDataSource {
  private list: MyPhotoData[] = []
  private listeners: DataChangeListener[] = [];

  constructor(list: MyPhotoData[]) {
    this.list = list
  }

  totalCount(): number {
    return this.list.length
  }

  getData(index: number): MyPhotoData {
    return this.list[index]
  }

  registerDataChangeListener(listener: DataChangeListener): void {
    this.listeners.push(listener)
  }

  unregisterDataChangeListener() {
  }

  notifyDataChange(index: number): void {
    this.listeners.forEach(listener => {
      listener.onDataChange(index);
    })
  }
}

class MyPhotoData {
  total: number = 10
  value: number = 0
  id: number = -1
}


@Entry
@Component
struct Index {
  private swiperController: SwiperController = new SwiperController()
  @State progressData: MyPhotoData[] = []
  @State data: MyDataSource = new MyDataSource([])
  @State currentIndex: number = -1
  @State duration: number = 3000
  @State swiperMaxHeight: number = 800
  @State progressHeight: number = 50
  scroller: Scroller = new Scroller()

  aboutToAppear(): void {
    let list: MyPhotoData[] = []
    for (let i = 1; i <= 5; i++) {
      let newPhotoData = new MyPhotoData()
      newPhotoData.id = i
      list.push(newPhotoData);
    }
    this.progressData = list
    this.data = new MyDataSource(list)
  }

  @Builder
  itemFoot() {
    Row({ space: 5 }) {
      ForEach(this.progressData, (item: MyPhotoData, index: number) => {
        Stack({ alignContent: Alignment.Start }) {
          Row()
            .zIndex(0)
            .width('100%')
            .height(4)
            .borderRadius(2)
            .backgroundColor("#19182431")
          Row()
            .zIndex(1)
            .width(this.currentIndex >= index ? '100%' : '0')
            .height(4)
            .borderRadius(2)
            .backgroundColor("#ff007dff")
            .animation({
              duration: this.duration - 400,
              curve: Curve.Linear,
              iterations: 1,
              playMode: PlayMode.Normal,
              onFinish: () => {
                if (this.currentIndex === this.progressData.length - 1) {
                  this.duration = 400
                  this.currentIndex = -1
                }
              }
            })
        }
        .layoutWeight(1)
      }, (item: MyPhotoData) => JSON.stringify(item))
    }
    .width('100%')
    .height(this.progressHeight)
  }

  build() {
    Column() {
      Row() {
        Text('导航栏')
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
      .height(56)

      Scroll() {
        Stack({ alignContent: Alignment.TopStart }) {
          List() {
            ListItemGroup({ footer: this.itemFoot() }) {
              ListItem() {
                Swiper(this.swiperController) {
                  LazyForEach(this.data, (item: MyPhotoData, index: number) => {
                    Image($r('app.media.496799071_thumb'))
                      .width('100%')
                      .height(this.swiperMaxHeight)
                  }, (item: MyPhotoData) => JSON.stringify(item))
                }
                .cachedCount(2)
                .index(0)
                .autoPlay(true)
                .interval(3000)
                .loop(true)
                .indicatorInteractive(true)
                .duration(300)
                .itemSpace(0)
                .indicator(false)
                .displayArrow(false)
                .curve(Curve.Linear)
                .onChange((index: number) => {
                  this.duration = 3000
                  this.currentIndex = index
                })
                .onAppear(() => {
                  this.currentIndex = 0
                })
              }
              .height(this.swiperMaxHeight - this.progressHeight)
            }

            ListItemGroup() {
              ListItem() {
                Text('文章区域')
                  .width('100%')
                  .height(500)
                  .backgroundColor(Color.Orange)
              }
            }
          }
          .width('100%')
          .height('100%')
          .sticky(StickyStyle.Footer)
        }
        .layoutWeight(1)
      }
      .layoutWeight(1)
      .backgroundColor(Color.White)


      Row() {
        Text('发表评论')
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
      .height(56)
      .backgroundColor(Color.White)
    }
    .height('100%')
    .width('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
分享
微博
QQ
微信
回复
2024-12-26 15:47:46
相关问题
如何实现带图片进度条
1392浏览 • 1回复 待解决
HarmonyOS 进度条样式
822浏览 • 1回复 待解决
HarmonyOS 怎样实现进度条
1030浏览 • 1回复 待解决
HarmonyOS 多彩色进度条展示
459浏览 • 1回复 待解决
基于Progress组件的进度条
1473浏览 • 1回复 待解决
HarmonyOS 环状渐变色进度条
779浏览 • 1回复 待解决
如何实现带刻度的进度条
1253浏览 • 1回复 待解决
弧形进度条实现,有人知道方法吗?
1225浏览 • 1回复 待解决
Progress进度条如何实现渐变色?
1817浏览 • 1回复 待解决
服务卡片的进度条如何停止动画
9760浏览 • 1回复 待解决
app切换到后台时进度条的处理的问题
3211浏览 • 0回复 待解决
实现一个发送进度条通知的方法
924浏览 • 1回复 待解决
怎么在进度条更新的时候刷新页面?
5565浏览 • 1回复 待解决