比lazyForEach 简单

@Entry
@ComponentV2
struct RepeatPage {
  @Local listData: string[] = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

  build() {
    Column() {
      List({space : 10}){
        Repeat<string>(this.listData)
          .each((repeatItem)=>{
            //这里需要手动打 提示不出来
            ListItem(){
              Text(repeatItem.item)
                .width('100%')
                .height(300)
                .backgroundColor(Color.Pink)
                .textAlign(TextAlign.Center)
            }
          })
          .virtualScroll({totalCount:this.listData.length})
          .key((item,index)=>{
            return item
          })
          .templateId((item,index)=>{
            return index%2 ===0 ?'':'aaa'
          })
          //自定义模板 默认样式为''
          .template('aaa',(repeatItem : RepeatItem<string>)=>{
            ListItem(){
              Text(repeatItem.item)
                .width('100%')
                .height(300)
                .backgroundColor(Color.White)
                .textAlign(TextAlign.Center)
            }
          },{cachedCount : 1})
      }.cachedCount(4)

    }
    .height('100%')
    .width('100%')
  }
}

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐