ArkUI List组件

我们在column中使用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 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),
    new Item('os6',$r('app.media.hongmeng'),7000)
  ]
  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)
  }
}

我们会发现超出高度的部分被隐藏了,这个问题我们可以使用List组件来解决。

List

List组件是一种复杂容器,具备以下特点

1.列表项(ListItem)数量过多超出屏幕后,会自动提供滚动功能。

2.列表项(ListItem)既可以纵向排列,也可以横向排列

List({space:10}){

ForEach([1,2,3,4]

, item=>{

ListItem(){

// 列表项内容,只能包含一个根组件。

Text("Listitem")

}

} )

}

.width('100%')

使用List 替代Column完成商品列表的布局

在定义List的时候,要注意高度的变化。一般List组件使用的是动态的高度。我们在这里可以使用 layoutWeight权重属性来控制高度的获取。当他为0时,就是默认设置的值,当他有值时候,根据权重获取剩下的高度。

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 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),
    new Item('os6',$r('app.media.hongmeng'),7000)
  ]
  build() {
    Column({space:8}){
      Row(){
        Text('商品列表')
          .fontSize(30)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
      .height(30)
      .margin({bottom: 20})

      List({space:8}){
        // 循环控制
        ForEach(
          this.items,
          item=>{
            ListItem(){
              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%')
      // 权重分配 0就是正常 否则权重高 除了你的 全是我的
      .layoutWeight(1)


    }
    .width('100%')
    .height('100%')
    .backgroundColor('#efefef')
    .padding(20)
  }
}
相关推荐
zwjapple3 小时前
docker-compose一键部署全栈项目。springboot后端,react前端
前端·spring boot·docker
像风一样自由20206 小时前
HTML与JavaScript:构建动态交互式Web页面的基石
前端·javascript·html
aiprtem6 小时前
基于Flutter的web登录设计
前端·flutter
浪裡遊6 小时前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
why技术6 小时前
Stack Overflow,轰然倒下!
前端·人工智能·后端
GISer_Jing7 小时前
0704-0706上海,又聚上了
前端·新浪微博
止观止7 小时前
深入探索 pnpm:高效磁盘利用与灵活的包管理解决方案
前端·pnpm·前端工程化·包管理器
whale fall7 小时前
npm install安装的node_modules是什么
前端·npm·node.js
烛阴7 小时前
简单入门Python装饰器
前端·python
袁煦丞8 小时前
数据库设计神器DrawDB:cpolar内网穿透实验室第595个成功挑战
前端·程序员·远程工作