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

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

HarmonyOS
16h前
浏览
收藏 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%')
  }
}
分享
微博
QQ
微信
回复
15h前
相关问题
如何实现带图片进度条
829浏览 • 1回复 待解决
HarmonyOS 怎样实现进度条
37浏览 • 1回复 待解决
HarmonyOS 多彩色进度条展示
23浏览 • 1回复 待解决
基于Progress组件的进度条
572浏览 • 1回复 待解决
HarmonyOS 环状渐变色进度条
44浏览 • 1回复 待解决
如何实现带刻度的进度条
710浏览 • 1回复 待解决
弧形进度条实现,有人知道方法吗?
784浏览 • 1回复 待解决
服务卡片的进度条如何停止动画
8859浏览 • 1回复 待解决
Progress进度条如何实现渐变色?
862浏览 • 1回复 待解决
app切换到后台时进度条的处理的问题
2609浏览 • 0回复 待解决
怎么在进度条更新的时候刷新页面?
4712浏览 • 1回复 待解决
实现一个发送进度条通知的方法
391浏览 • 1回复 待解决
如何实现一个月食样式的进度条
371浏览 • 1回复 待解决