ArkTS循环控制与List的使用

ArkTS如何进行循环渲染

现有数据如下

ts 复制代码
class Item{
  name:string
  image:Resource
  price:number
  dicount:number
  constructor(name:string,image:Resource,price:number,dicount?:number) {
    this.name = name
    this.image = image
    this.price = price
    this.dicount = dicount
  }
}

private items:Array<Item> = [
  new Item('商品1',$r('app.media.phone1'),6999,500),
  new Item('商品2',$r('app.media.phone2'),10999,1000),
  new Item('商品3',$r('app.media.phone3'),999),
  new Item('商品4',$r('app.media.phone4'),1699),
  new Item('商品5',$r('app.media.phone5'),2699),
  new Item('商品6',$r('app.media.phone6'),1699,100),
  new Item('商品7',$r('app.media.phone7'),699),
  new Item('商品8',$r('app.media.phone8'),16999),
]

直接使用ForEach遍历渲染

ForEach(arr:Array,(item:any,index?:number) => {//结构},keyGenerator?:(item:any,index?:number):string => {})

分析三个参数

  1. arr

    需要遍历的数组

  2. (item:any,index?:number) => {// 结构}

    页面组件生成函数

    item默认类型为any,index为可选参数(数组角标),函数内部放结构体部分。

  3. keyGenerator?:(item:any,index?:number):string => {}

    键生成函数,为数组每一项生成一个唯一标识,组件是否重新渲染的判断标准(如果只有一项发生改变,不会重新渲染其他元素)。

ts 复制代码
ForEach(
  this.items,
  (item:Item)=>{
    Row({space:20}){
      Image(item.image).width(100)
      Column({space:4}){
        if(item.dicount && item.dicount > 0){
          Text(item.name).fontSize(20).fontWeight(FontWeight.Bold)
          Text("原价:¥"+item.price).fontColor('#ccc').fontSize(16).decoration({type:TextDecorationType.LineThrough})
          Text("折扣价:¥"+(item.price-item.dicount)).fontColor('#f36').fontSize(18)
          Text("补贴:¥"+item.dicount).fontColor('#f36').fontSize(16)
        }else{
          Text(item.name).fontSize(20).fontWeight(FontWeight.Bold)
          Text("¥"+item.price).fontColor('#f36').fontSize(18)
        }
      }
      .height('100%')
      .alignItems(HorizontalAlign.Start)
    }
    .width('100%')
    .height(120)
    .backgroundColor("#FFF")
    .borderRadius(20)
    .padding(10)
  }
)

问题

直接使用ForEach循环控制,如果元素内容超过视口高度,不会自动生成滚动条,如果超出部分需要滚动条,可以使用List组件。

List组件

List组件相比ForEach,如果元素遍历后元素高度超过视口高度后,会自动产生滚动条。

List(){

​ ListItem(){

​ // 结构

​ }

}

将ForEach和List组件结合使用即可

需要注意的是,使用List组件时,必须通过ListItem才能渲染结构

ts 复制代码
List(){
	ForEach(
		arr,
		(item:Item) => {
			ListItem(){
				// 结构
			}
		}
	)
}
相关推荐
见山是山-见水是水4 小时前
鸿蒙Divider 分割线组件完全指南:内容分组、视觉分区与自定义样式
华为·harmonyos
贾伟康5 小时前
【知律|18】HarmonyOS ArkTS 权限与隐私实战:让 module.json5、功能说明和拒绝路径一致
harmonyos·arkts·隐私合规·appgallery·应用权限
Kevin Coding16 小时前
JsonConvert:适用于 Android、鸿蒙与 Flutter 的 JSON 转 Model 插件
android·flutter·harmonyos
m0_7496902318 小时前
【寻迹校园 HarmonyOS NEXT 实战 28】不交换手机号也能交接:固定校内交接点的隐私设计
华为·harmonyos·arkts·产品设计·隐私设计·安全交接
Magic-ZYJ18 小时前
HarmonyOS Stage 模型实战:UIAbility 生命周期如何驱动页面安全状态
安全·华为·harmonyos·鸿蒙·移动端开发·独立开发者·心晴手记
贾伟康18 小时前
【中国方言题库|09】HarmonyOS ArkTS 方言搜索实战:实现词语检索和无结果反馈
harmonyos·arkts·状态管理·arkui·本地搜索
Dovis(誓平步青云)19 小时前
从手机单栏到平板分栏:任务清单的筛选、选中态与不可变更新
华为·harmonyos
梦想不只是梦与想19 小时前
鸿蒙 AppGallery Connect:查看应用信息(三)
harmonyos·appgallery·client id·app id·developer id
贾伟康19 小时前
【中国方言题库|03】HarmonyOS ArkTS 四川话分库实战:复用题库组件并保持地区参数清晰
harmonyos·arkts·arkui·路由传参·组件复用
贾伟康20 小时前
【中国方言题库|10】HarmonyOS ArkTS 语音播放实战:管理读音播放与页面生命周期
生命周期·harmonyos·arkts·语音合成·texttospeech