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)
  }
}
相关推荐
imLix1 分钟前
RunLoop 实现原理
前端·ios
wayman_he_何大民7 分钟前
初始机器学习算法 - 关联分析
前端·人工智能
飞飞飞仔11 分钟前
别再瞎写提示词了!这份Claude Code优化指南让你效率提升10倍
前端·claude
刘永胜是我11 分钟前
node版本切换
前端·node.js
成小白15 分钟前
前端实现表格下拉框筛选和表头回显和删除
前端
wayman_he_何大民16 分钟前
初始机器学习算法 - 聚类分析
前端·人工智能
wycode18 分钟前
Vue2实践(3)之用component做一个动态表单(二)
前端·javascript·vue.js
用户10922571561033 分钟前
你以为的 Tailwind 并不高效,看看这些使用误区
前端
意会1 小时前
微信闪照小程序实现
前端·css·微信小程序
onejason1 小时前
《利用 Python 爬虫获取 Amazon 商品详情实战指南》
前端·后端·python