【HarmonyOS4学习笔记】《HarmonyOS4+NEXT星河版入门到企业级实战教程》课程学习笔记(六)

课程地址: 黑马程序员HarmonyOS4+NEXT星河版入门到企业级实战教程,一套精通鸿蒙应用开发

(本篇笔记对应课程第 12 - 13节)

P12《11.ArkUI组件-循环控制》

forEach() 方法的使用方式:

在预览界面点击红框的按钮,可以查看页面组件树结构:


将写好的静态代码改为使用forEach()循环渲染多项:

页面中定义好数据:

使用forEach()遍历数据:

需求新增:

总结:

实践:

typescript 复制代码
class Item {
  name : string
  image : ResourceStr
  price : number
  discount : number

  constructor(name:string, image:ResourceStr, price:number, discount:number=0) {
    this.name = name
    this.image = image
    this.price = price
    this.discount = discount
  }
}

@Entry
@Component
struct ItemsPage {
  // 商品数据
  private items: Array<Item> = [
    new Item('FreeBuds 5i', $r('app.media.1'), 449),
    new Item('FreeBuds Pro 3典藏版', $r('app.media.2'), 1449, 500),
    new Item('问界 新M5', $r('app.media.3'), 291800),
    new Item('WATCH 4 Pro', $r('app.media.4'), 4999),
    new Item('华为智慧屏 S5', $r('app.media.5'), 5799)
  ]

  build(){
    Column(){
      Row(){
        Text('商品列表')
          .fontSize(28)
      }
        .width('100%')
        .height(60)
        .padding({left:14, right:14})
        .justifyContent(FlexAlign.Start)

      Column({space:10}){
        ForEach(this.items,
          (item:Item) => {
            Row({space:10}){
              Image(item.image)
                .width(100)
              Column(){
                Text(item.name)
                  .fontSize(18)
                  .fontColor('#444')
                  .fontWeight(FontWeight.Bold)

                if(item.discount){
                  Text('原价¥:' + item.price)
                    .fontSize(18)
                    .fontColor('#888')
                    .decoration({type:TextDecorationType.LineThrough})
                  Text('折扣价¥:' + (item.price - item.discount))
                    .fontSize(18)
                    .fontColor('#6d6d')
                  Text('补贴¥:' + item.discount)
                    .fontSize(18)
                    .fontColor('#6d6d')
                }else{
                  Text('¥:' + item.price)
                    .fontSize(18)
                    .fontColor('#6d6d')
                }


              }
                .alignItems(HorizontalAlign.Start)
            }
            .width('100%')
            .padding({left:14, right:14})
            .backgroundColor('#fff')
            .borderRadius(8)
          }
        )
      }
        .padding({left:10,right:10})


    }
      .width('100%')
      .height('100%')
      .backgroundColor('#eee')
  }
}

P13《12.ArkUI组件-List》

Column布局当列表数量过多超出屏幕之后,不能支持滚动效果:

这时就需要用List组件替代Column组件了。注意:List是容器组件,它里面一定要有一个 ListItem ,但ListItem 不是容器组件。

实践:

typescript 复制代码
class Item {
  name : string
  image : ResourceStr
  price : number
  discount : number

  constructor(name:string, image:ResourceStr, price:number, discount:number=0) {
    this.name = name
    this.image = image
    this.price = price
    this.discount = discount
  }
}

@Entry
@Component
struct ItemsPage {
  // 商品数据
  private items: Array<Item> = [
    new Item('FreeBuds 5i', $r('app.media.1'), 449),
    new Item('FreeBuds Pro 3典藏版', $r('app.media.2'), 1449, 500),
    new Item('问界 新M5', $r('app.media.3'), 291800),
    new Item('WATCH 4 Pro', $r('app.media.4'), 4999),
    new Item('华为智慧屏 S5', $r('app.media.5'), 5799),
    new Item('华为智慧屏 S5', $r('app.media.5'), 5799),
    new Item('华为智慧屏 S5', $r('app.media.5'), 5799),
    new Item('华为智慧屏 S5', $r('app.media.5'), 5799)
  ]

  build(){
    Column(){
      Row(){
        Text('商品列表')
          .fontSize(28)
      }
        .width('100%')
        .height(60)
        .padding({left:14, right:14})
        .justifyContent(FlexAlign.Start)

      List({space:10}){
        ForEach(this.items,
          (item:Item) => {
            ListItem(){
              Row({space:10}){
                Image(item.image)
                  .width(100)
                Column(){
                  Text(item.name)
                    .fontSize(18)
                    .fontColor('#444')
                    .fontWeight(FontWeight.Bold)

                  if(item.discount){
                    Text('原价¥:' + item.price)
                      .fontSize(18)
                      .fontColor('#888')
                      .decoration({type:TextDecorationType.LineThrough})
                    Text('折扣价¥:' + (item.price - item.discount))
                      .fontSize(18)
                      .fontColor('#6d6d')
                    Text('补贴¥:' + item.discount)
                      .fontSize(18)
                      .fontColor('#6d6d')
                  }else{
                    Text('¥:' + item.price)
                      .fontSize(18)
                      .fontColor('#6d6d')
                  }


                }
                .alignItems(HorizontalAlign.Start)
              }
              .width('100%')
              .padding({left:14, right:14})
              .backgroundColor('#fff')
              .borderRadius(8)
            }
          }
        )
      }
        .padding({left:10,right:10})
        .layoutWeight(1)


    }
      .width('100%')
      .height('100%')
      .backgroundColor('#eee')
  }
}
相关推荐
paopaokaka_luck1 小时前
基于springboot3+vue3的企业考勤管理系统(部门树递归、Echarts图形化分析)
开发语言·spring boot·学习·echarts·mybatis·需求分析·代码规范
minglie11 小时前
zynq高频小数据量PS闭环 抖动时间的测量
学习
V哥AI增长1 小时前
小红书笔记在生成式AI引擎中的可见性机制解析:从抓取原理到GEO工程化实践
大数据·人工智能·笔记
long3161 小时前
MySQL 学习练习(配套 01 入门资料)
数据库·学习·mysql
梦雨生生2 小时前
java开发工具(学习第一天)
java·开发语言·学习
条tiao条4 小时前
MVVM架构与ArkUI状态管理
华为·架构·harmonyos·鸿蒙·mvvm
Hotchip_MEMS5 小时前
当雾化器遇上MEMS:一场从交互到制造的全链路效率提升
人工智能·笔记·物联网·电脑·制造
是上好佳佳佳呀6 小时前
【深度学习|Day02】PyTorch 深度学习笔记(下):张量运算与自动微分
pytorch·笔记·深度学习
IT古董7 小时前
【MES学习笔记系列】05 - MES 数据库设计
笔记·学习·mes
不瘦80斤不改名8 小时前
05-vibe-coding-向agentic-engineering演进
人工智能·笔记·python·prompt