ArkTS 循环控制

 在ArkTS开发中,我们使用ForEach / if-else 来控制页面的循环布局。

 ForEach

ForEach(

arr:Array, //数组

(item:any,index?:number)=>{

        Row(){

            ...

        }

},

keyGenerator?:(item: any,index?: number): string => {。  // 非常重要 大多数情况下不传

       // 键生成函数 为数组每一项生成一个唯一标识,作为组件是否重新渲染的判断标准。

        }

)

使用foreach 循环渲染一个商品页

这里我们构建一个类,写出一个数组来,将数组作为参数穿入下面的foreach中,完成循环渲染出一个商品列表。 

class Item {
  name: string
  image: ResourceStr
  price: number

  constructor(name: string, image: ResourceStr, price: number){
    this.name = name
    this.image = image
    this.price = price
  }
}
@Entry
@Component
struct Index {
  private items: Array<Item> = [
    new Item('os1',$r('app.media.hongmeng'),2000),
    new Item('os2',$r('app.media.hongmeng'),3000),
    new Item('os3',$r('app.media.hongmeng'),4000),
    new Item('os4',$r('app.media.hongmeng'),5000),
    new Item('os5',$r('app.media.hongmeng'),6000)
  ]
  build() {
    Column({space:8}){
      Row(){
        Text('商品列表')
          .fontSize(30)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
      .margin({bottom: 20})

      // 循环控制
      ForEach(
        this.items,
        item=>{
          Row({space:10}){
            Image(item.image)
              .width(100)
            Column({space: 4}){
              Text(item.name)
                .fontSize(20)
                .fontWeight(FontWeight.Bold)
              Text('¥' + item.price)
                .fontSize(18)
                .fontColor('#ff0000')
            }
            .height('100%')
            .alignItems(HorizontalAlign.Start)
          }
          .width('100%')
          .backgroundColor('#fff')
          .borderRadius(20)
          .height(120)
          .padding(10)
        }
      )
    }
    .width('100%')
    .height('100%')
    .backgroundColor('#efefef')
    .padding(20)
  }
}

 if-else

if(判断条件){

 //内容

}else{

//内容

}

通过判断条件决定使用哪种方式渲染

 通过if-else来完成折扣价格的实现

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 Index {
  private items: Array<Item> = [
    new Item('os1',$r('app.media.hongmeng'),2000,500),
    new Item('os2',$r('app.media.hongmeng'),3000),
    new Item('os3',$r('app.media.hongmeng'),4000,300),
    new Item('os4',$r('app.media.hongmeng'),5000),
    new Item('os5',$r('app.media.hongmeng'),6000)
  ]
  build() {
    Column({space:8}){
      Row(){
        Text('商品列表')
          .fontSize(30)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
      .margin({bottom: 20})

      // 循环控制
      ForEach(
        this.items,
        item=>{
          Row({space:10}){
            Image(item.image)
              .width(100)
            Column({space: 4}){
              if(item.discount){
                // 名字
                Text(item.name)
                  .fontSize(20)
                  .fontWeight(FontWeight.Bold)
                // 原价
                Text('原价: ¥ ' + item.price)
                  .fontSize(16)
                  .fontColor('#ccc')
                  // 装饰效果
                  .decoration({type:TextDecorationType.LineThrough})
                // 折扣价
                Text('折扣价: ¥ '+(item.price - item.discount))
                  .fontSize(18)
                  .fontColor('#ff0000')
                // 补贴信息
                Text('补贴: ¥ ' + item.discount)
                  .fontSize(18)
                  .fontColor('#ff0000')
              }else{
                Text(item.name)
                  .fontSize(20)
                  .fontWeight(FontWeight.Bold)
                Text('¥' + item.price)
                  .fontSize(18)
                  .fontColor('#ff0000')
              }

            }
            .height('100%')
            .alignItems(HorizontalAlign.Start)
          }
          .width('100%')
          .backgroundColor('#fff')
          .borderRadius(20)
          .height(120)
          .padding(10)
        }
      )
    }
    .width('100%')
    .height('100%')
    .backgroundColor('#efefef')
    .padding(20)
  }
}

 

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
ARKTS温度控制界面是一种用于控制和监测温度的用户界面。它可以通过图形化显示温度数据和各种控制选项来帮助用户轻松管理和调整温度设置。 首先,ARKTS温度控制界面提供了实时温度监测功能。用户可以在界面上实时查看当前温度,并根据需要进行相应的调整。这使得用户能够及时了解到温度变化,从而采取有效的控制措施。 其次,界面上还提供了温度调节选项。用户可以通过界面上的控制按钮或滑块来调整相应的温度设置。比如,用户可以通过界面上的调整按钮来提高或降低温度、设置温度范围等。这样,用户可以根据自身需求和环境要求进行灵活的温度调节。 此外,ARKTS温度控制界面还具备报警功能。当温度超出设定的范围时,界面会提醒用户及时采取措施。这种报警功能对于保障设备和材料的安全性至关重要。用户可以在界面上设置报警阈值,并选择接收报警通知的方式,如声音、短信或电子邮件等。 最后,ARKTS温度控制界面具有可视化的数据显示功能。它可以将温度数据以图表或曲线的形式展示出来,使用户能够直观地了解温度的变化趋势。这样,用户可以更好地判断温度是否稳定,是否需要进行调整。 总之,ARKTS温度控制界面通过实时监测、温度调节、报警和数据可视化等功能,为用户提供了便捷、灵活和高效的温度控制方式。它在工业、实验室和家庭等多个领域都有广泛应用,帮助用户实现温度的精确控制和调节。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Web阿成

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值