鸿蒙开发系列教程(九)--ArkTS语言:ForEach循环渲染

18 篇文章 4 订阅
12 篇文章 0 订阅

ForEach:循环渲染

官方:ForEach接口基于数组类型数据来进行循环渲染,需要与容器组件配合使用,且接口返回的组件应当是允许包含在ForEach父容器组件中的子组件。

语法:

ForEach(

arr: Array,

itemGenerator: (item: any, index?: number) => void,

​ keyGenerator?: (item: any, index?: number) => string

)

参数解释:

1、arr: Array 要遍历的数据,数组

2、itemGenerator: (item: any, index?: number) => void,

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

​ 组件生成函数(数组中的数据项,数组中的数据项索引-可省略)

返回函数或void

3、 keyGenerator?: (item: any, index?: number) => string

可省略,键值生成函数(数组中的数据项,数组中的数据项索引-可省略)

如果函数缺省,框架默认的键值生成函数为(item: T, index: number) => { return index + ‘__’ + JSON.stringify(item); }

示例1:

@State simpleList: Array<string> = ['one', 'two', 'three'];

ForEach(this.simpleList, (item2: string) => {
          Text(item2).fontColor(Color.Red)
        },)

在这里插入图片描述

示例2:**ForEach与list组件整合

@Entry
@Component
struct ForEachListItem {
  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

  build() {
    Column() {
      List({ space: 20, initialIndex: 0 }) {
        ForEach(this.arr, (item) => {
          //创建列表项
          ListItem() {
            Text('列表' + item)
              .width('100%').height(100).fontSize(16)
              .textAlign(TextAlign.Center).borderRadius(10).backgroundColor(0xFFFFFF)
          }
        }, item => item)
      }.width('90%')
    }.width('100%').height('100%')
    .backgroundColor(0xf5a0a3)
    .padding({ top: 5 })
  }
}

在这里插入图片描述

示例3:** ForEach与自定义组件

@Entry
@Component
struct Parent {
  @State simpleList: Array<string> = ['one', 'two', 'three'];

  build() {
    Row() {
      Column() {
        ForEach(this.simpleList, (item: string) => {
          ChildItem({ item1: item })
        }, (item: string) => item)
      }
      .width('100%')
      .height('100%')
    }
    .height('100%')
    .backgroundColor(0xfceace)
  }
}

@Component
struct ChildItem {
  @Prop item1: string;
  build() {
    Text('父组件参数:'+this.item1)
      .fontSize(20)
  }
}

在这里插入图片描述

**示例4 ** 创建类对象,渲染组件,综合案例

定义类:

// 定义类--文章类
class MyArticle {
  id : string
  title : string
  content : string
  //构造函数
  constructor(id: string, title: string, content: string) {
    this.id = id
    this.title = title
    this.content = content
  
  }
}

具体实现:

@Entry
@Component
struct MyArticleListView {
  @State isListReachEnd: boolean = false;
  @State MyArticleList: Array<MyArticle> = [
    new MyArticle('001', '第1篇文章', '文章简介内容'),
    new MyArticle('002', '第2篇文章', '文章简介内容'),
    new MyArticle('003', '第3篇文章', '文章简介内容'),
    new MyArticle('004', '第4篇文章', '文章简介内容'),
    new MyArticle('005', '第5篇文章', '文章简介内容'),
    new MyArticle('006', '第6篇文章', '文章简介内容')
  ]

  loadMoreMyArticles() {
    this.MyArticleList.push(new MyArticle('007', '加载的新文章', '文章简介内容'));
  }

  build() {
    Column({ space: 5 }) {
      List() {
        ForEach(this.MyArticleList, (item: MyArticle) => {
          ListItem() {
            MyArticleCard({ MyArticle: item })
              .margin({ top: 20 })
          }
        }, (item: MyArticle) => item.id)
      }
      .onReachEnd(() => {
        this.isListReachEnd = true;
      })
      .parallelGesture(
        PanGesture({ direction: PanDirection.Up, distance: 80 })
          .onActionStart(() => {
            if (this.isListReachEnd) {
              this.loadMoreMyArticles();
              this.isListReachEnd = false;
            }
          })
      )
      .padding(20)
      .scrollBar(BarState.Off)
    }
    .width('100%')
    .height('100%')
    .backgroundColor(0xF1F3F5)
  }
}

@Component
struct MyArticleCard {
  @Prop MyArticle: MyArticle;

  build() {
    Row() {
      Image($r('app.media.icon'))
        .width(80)
        .height(80)
        .margin({ right: 20 })

      Column() {
        Text(this.MyArticle.title)
          .fontSize(20)
          .margin({ bottom: 8 })
        Text(this.MyArticle.content)
          .fontSize(16)
          .fontColor(Color.Gray)
          .margin({ bottom: 8 })
      }
      .alignItems(HorizontalAlign.Start)
      .width('80%')
      .height('100%')
    }
    .padding(20)
    .borderRadius(12)
    .backgroundColor('#FFECECEC')
    .height(120)
    .width('100%')
    .justifyContent(FlexAlign.SpaceBetween)
  }
}

在这里插入图片描述

  • 10
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值