ArkUI-08-List

import { createCollaborationCameraMenuItems } from '@hms.collaboration.camera'
import { Type } from '@ohos.arkui.StateManagement'

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

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


}
@Entry
@Component
struct Index {

  private items:Array<Item> = [
    new Item('锅包肉',$r('app.media.test'),38,18),
    new Item('溜肉段',$r('app.media.test'),40,0),
    new Item('爆炒土豆丝',$r('app.media.test'),43,0),
    new Item('回锅肉',$r('app.media.test'),49,0),
    new Item('手把凉菜',$r('app.media.test'),51,0),
    new Item('手把凉菜',$r('app.media.test'),51,0)
  ]
  build() {
    Column({space:8}){
      Row(){
        Text('食品列表')
          .fontSize(30)
          .fontWeight(FontWeight.Bold)
      }.padding({left:20})
      .width('100%')
      .height(30)
      .margin({bottom:20})
      // .backgroundColor('#ffb7d4b6')

      List({space: 8}){
        ForEach(this.items, (item:Item,index?:number) => {
         ListItem(){

           Row({space:10}){
             Image(item.image)
               .width(100)

             Column({space:4}){
               if(item.discount != 0){
                 Text(item.name)
                   .fontSize(20)
                   .fontWeight(FontWeight.Bold)

                 Text('原价:¥ '+ item.price)
                   .fontColor('#CCC')
                   .fontSize(18)
                   .decoration({type: TextDecorationType.LineThrough})

                 Text('折扣价:¥ '+ (item.price-item.discount))
                   .fontColor('#F36')
                   .fontSize(18)
                 Text('补贴:¥ '+ item.discount)
                   .fontColor('#F36')
                   .fontSize(18)
               }else{
                 Text(item.name)
                   .fontSize(20)
                   .fontWeight(FontWeight.Bold)

                 Text('$ '+ item.price)
                   .fontColor('#F36')
                   .fontSize(18)
               }

             }
             .height('100%')
             .alignItems(HorizontalAlign.Start)
           }.padding(10)
           .width('80%')
           .height(120)
           .backgroundColor('#ffffffff')
           .borderRadius(15)
         }
        }, (item: string) => item)

      }
      .width('100%')
      .alignListItem(ListItemAlign.Center)
      .layoutWeight(1)






    }
    .backgroundColor('#ffcdd6ea')
    .width('100%')

  }

}

1. List 组件

  • 作用List 组件用于承载一组 ListItem 组件。它通常用于显示滚动的列表数据。
  • 数据绑定List 需要绑定数据源,这通常是一个数组。通过数据绑定,List 会动态生成对应数量的 ListItem
  • 性能优化:如果 List 包含大量数据项,建议使用虚拟化技术(如懒加载)来提高性能。鸿蒙的 List 组件通常会自动处理虚拟化,但在处理大量数据时仍需关注性能。

2. ListItem 组件

  • 作用ListItem 组件用于定义列表中的每一项。它通常包括显示内容、布局样式等。
  • 结构和样式:每个 ListItem 可以包含不同的子组件和样式来满足设计需求。确保 ListItem 的布局和样式符合设计规范。
  • 交互:如果 ListItem 需要支持用户交互(如点击事件),确保事件处理逻辑正确且性能良好。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值