鸿蒙NEXT开发-界面渲染(条件和循环)(基于最新api12稳定版)

注意:博主有个鸿蒙专栏,里面从上到下有关于鸿蒙next的教学文档,大家感兴趣可以学习下

如果大家觉得博主文章写的好的话,可以点下关注,博主会一直更新鸿蒙next相关知识

专栏地址: https://blog.csdn.net/qq_56760790/category_12794123.html

目录

1. 渲染-条件渲染

1.1 基本介绍

1.2 使用if/else(创建销毁元素)

1.3 visibility属性控制

2. 渲染-循环渲染

2.1 基本介绍

2.2 首次渲染

2.3 非首次渲染

3. 学习地址


1. 渲染-条件渲染

1.1 基本介绍

在ArkTS中 我们要根据某个状态来控制元素或者组件的显示隐藏 可以采用条件渲染

1.2 使用if/else(创建销毁元素)

代码示例

@Entry
  @Component
  struct Index {

    @State isShow:boolean=true
    build() {
      Column() {
        Button('显示/隐藏')
          .width(100)
          .height(30)
          .onClick(()=>{
            if(this.isShow){
              this.isShow=false
            }else{
              this.isShow=true
            }
          })
        if(this.isShow){
          Text('我是东林').width(200).height(200).fontSize(40)
        }
      }.width('100%')
        .height('100%')
    }
  }

1.3 visibility属性控制

visibility属性有以下三种:

1、Visible 显示

2、Hidden 隐藏

3、None 隐藏,但是不占位置

代码示例

@Entry
  @Component
  struct Index {

    @State isShow:boolean=true
    build() {
      Column() {
        Button('显示/隐藏')
          .width(100)
          .height(30)
          .onClick(()=>{
            if(this.isShow){
              this.isShow=false
            }else{
              this.isShow=true
            }
          })
        Text('我是东林').width(200).height(200).fontSize(40)
          .backgroundColor(Color.Green)
          .visibility(this.isShow?Visibility.Visible:Visibility.Hidden)

        Text('小头').width(200).height(200).fontSize(40)
          .backgroundColor(Color.Yellow)
      }.width('100%')
        .height('100%')
    }
  }

2. 渲染-循环渲染

2.1 基本介绍

循环渲染使用 ForEach方法来进行

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

官方参考文档

文档中心

语法结构

ForEach(
  // 数据源
  arr: Array,
  // 组件生成函数
  itemGenerator: (item: 单项, index?: number) => void,
    // 键值生成函数
    keyGenerator?: (item: 单项, index?: number): string => string
                 )

代码示例

import FruitModel from '../model/FruitModel';

@Entry
  @Component
  struct Index {
    @State fruits: FruitModel[]=[new FruitModel('1','苹果','100')
                                 ,new FruitModel('2','香蕉','90'),
                                 new FruitModel('3','西瓜','200')];

    build() {
      Row() {
        Column() {
          ForEach(this.fruits, (item: FruitModel) => {
            Text(`${item.id}:${item.name}:${item.vote}`)
              .width(200)
              .height(200)
          }, (item: FruitModel) => item.id)
        }
        .width('100%')
          .height('100%')
      }
      .height('100%')
    }
  }

2.2 首次渲染

在ForEach首次渲染时,会根据前述键值生成规则为数据源的每个数组项生成唯一键值,并创建相应的组件。

@Entry
@Component
struct Index {
  @State simpleList: Array<string> = ['苹果', '香蕉', '西瓜'];

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

@Component
struct ChildItem {
  @Prop item: string;

  build() {
    Text(this.item)
      .fontSize(50)
  }
}

在上述代码中,键值生成规则是keyGenerator函数的返回值item。在ForEach渲染循环时,为数据源数组项依次生成键值苹果、香蕉和西瓜,并创建对应的ChildItem组件渲染到界面上。

当不同数组项按照键值生成规则生成的键值相同时,框架的行为是未定义的。例如,在以下代码中,ForEach渲染相同的数据项香蕉时,只创建了一个ChildItem组件,而没有创建多个具有相同键值的组件。

@Entry
  @Component
  struct Index {
    @State simpleList: Array<string> = ['苹果', '香蕉', '香蕉','西瓜'];

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

@Component
  struct ChildItem {
    @Prop item: string;

    build() {
      Text(this.item)
        .fontSize(50)
    }
  }

在该示例中,最终键值生成规则为item。当ForEach遍历数据源simpleList,遍历到索引为1的香蕉时,按照最终键值生成规则生成键值为香蕉的组件并进行标记。当遍历到索引为2的香蕉时,按照最终键值生成规则当前项的键值也为香蕉,此时不再创建新的组件。

2.3 非首次渲染

在ForEach组件进行非首次渲染时,它会检查新生成的键值是否在上次渲染中已经存在。如果键值不存在,则会创建一个新的组件;如果键值存在,则不会创建新的组件,而是直接渲染该键值所对应的组件。例如,在以下的代码示例中,通过点击事件修改了数组的第三项值为"西瓜test",这将触发ForEach组件进行非首次渲染。

@Entry
  @Component
  struct Index {
    @State simpleList: Array<string> = ['苹果', '香蕉','西瓜'];

    build() {
      Row() {
        Column() {
          Text('点击修改第3个数组项的值')
            .fontSize(24)
            .fontColor(Color.Red)
            .onClick(() => {
              this.simpleList[2] = '西瓜test';
            })

          ForEach(this.simpleList, (item: string) => {
            ChildItem({ item: item })
          }, (item: string) => item)
        }
        .width('100%')
          .height('100%')
      }
      .height('100%')
    }
  }

@Component
  struct ChildItem {
    @Prop item: string;

    build() {
      Text(this.item)
        .fontSize(50)
    }
  }

3. 学习地址

全网首发鸿蒙NEXT星河版零基础入门到实战,2024年最新版,企业级开发!视频陆续更新中!_哔哩哔哩_bilibili

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东林知识库

你的鼓励是我原创最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值