当前位置: 首页 > news >正文

ppt做书模板下载网站网站建站开发

ppt做书模板下载网站,网站建站开发,航空网站建设,太原整站优化ArkUI开发框架是一套构建 HarmonyOS / OpenHarmony 应用界面的声明式UI开发框架,它支持程序使用 if/else 条件渲染, ForEach 循环渲染以及 LazyForEach 懒加载渲染。本节笔者介绍一下这三种渲染方式的使用。 if/else条件渲染 使用 if/else 进行条件渲染…

ArkUI开发框架是一套构建 HarmonyOS / OpenHarmony 应用界面的声明式UI开发框架,它支持程序使用 if/else 条件渲染, ForEach 循环渲染以及 LazyForEach 懒加载渲染。本节笔者介绍一下这三种渲染方式的使用。

if/else条件渲染

使用 if/else 进行条件渲染需要注意以下情况:

  • if 条件语句可以使用状态变量。

  • 使用 if 可以使子组件的渲染依赖条件语句。

  • 必须在容器组件内使用。

  • 某些容器组件限制子组件的类型或数量。将if放置在这些组件内时,这些限制将应用于 ifelse 语句内创建的组件。例如,Grid 组件的子组件仅支持 GridItem 组件,在 Grid 组件内使用条件渲染时,则 if 条件语句内仅允许使用 GridItem 组件。

    简单样例如下所示:

    @Entry @Component struct ComponentTest {@State showImage: boolean = false;build() {Column({space: 10}) {if (this.showImage) {            // 显示图片Image($r("app.media.test")).width(160).height(60).backgroundColor(Color.Pink)} else {                         // 显示文本Text('Loading...').fontSize(23).width(160).height(60).backgroundColor(Color.Pink)}Button(this.showImage ? 'Image Loaded' : 'Load Image')    // 按钮文字.size({width: 160, height: 40}).backgroundColor(this.showImage ? Color.Gray : '#aabbcc')// 按钮背景色.onClick(() => {this.showImage = true;                                 // 设置标记位})}.width('100%').height('100%').padding(10)}
    }
    

    样例运行结果如下图所示:

    2_4_1

ForEach循环渲染

ArkUI开发框架提供循环渲染(ForEach组件)来迭代数组,并为每个数组项创建相应的组件。

ForEach 定义如下:

interface ForEach {(arr: Array<any>, itemGenerator: (item: any, index?: number) => void,keyGenerator?: (item: any, index?: number) => string): ForEach;
}
  • arr:必须是数组,允许空数组,空数组场景下不会创建子组件。

  • itemGenerator:子组件生成函数,为给定数组项生成一个或多个子组件。

  • keyGenerator:匿名参数,用于给定数组项生成唯一且稳定的键值。

    简单样例如下所示:

    @Entry @Component struct ComponentTest {private textArray: string[] = ["1", "2", "3", "4", "5"];        // 数据源build() {Column({space: 10}) {ForEach(this.textArray, (item: string, index?: number) => { // 循环数组创建每一个ItemText(`Text: ${item}`)                                     // 可以生成一个或多个子组件.fontSize(20).backgroundColor(Color.Pink).margin({ top: 10 })})}.width('100%').height('100%').padding(10)}
    }
    

    样例运行结果如下图所示:

    2_4_2

鸿蒙OS开发更多内容↓点击 《鸿蒙NEXT星河版开发学习文档》HarmonyOS与OpenHarmony技术

LazyForEach循环渲染

搜狗高速浏览器截图20240326151547.png

ArkUI开发框架提供数据懒加载( LazyForEach 组件)从提供的数据源中按需迭代数据,并在每次迭代过程中创建相应的组件。

  1. LazyForEach 定义如下:

    // LazyForEach定义
    interface LazyForEach {(dataSource: IDataSource, itemGenerator: (item: any, index?: number) => void,keyGenerator?: (item: any, index?: number) => string): LazyForEach;
    }// IDataSource定义
    export declare interface IDataSource {totalCount(): number;getData(index: number): any;registerDataChangeListener(listener: DataChangeListener): void;unregisterDataChangeListener(listener: DataChangeListener): void;
    }// DataChangeListener定义
    export declare interface DataChangeListener {onDataReloaded(): void;onDataAdded(index: number): void;onDataMoved(from: number, to: number): void;onDataDeleted(index:number): void;onDataChanged(index:number): void;
    }
    
    • itemGenerator:子组件生成函数,为给定数组项生成一个或多个子组件。
    • keyGenerator:匿名参数,用于给定数组项生成唯一且稳定的键值。
    • dataSource:实现 IDataSource 接口的对象,需要开发者实现相关接口。
  2. IDataSource 定义如下:

    export declare interface IDataSource {totalCount(): number;getData(index: number): any;registerDataChangeListener(listener: DataChangeListener): void;unregisterDataChangeListener(listener: DataChangeListener): void;
    }
    
    • totalCount:获取数据总数。
    • getData:获取索引对应的数据。
    • registerDataChangeListener:注册改变数据的监听器。
    • unregisterDataChangeListener:注销改变数据的监听器。
  3. DataChangeListener 定义如下:

    export declare interface DataChangeListener {onDataReloaded(): void;onDataAdded(index: number): void;onDataMoved(from: number, to: number): void;onDataDeleted(index:number): void;onDataChanged(index:number): void;
    }
    
    • onDataReloaded:item重新加载数据时的回调。
    • onDataAdded:item新添加数据时的回调。
    • onDataMoved:item数据移动时的回调。
    • onDataDeleted:item数据删除时的回调。
    • onDataChanged:item数据变化时的回调。

简单样例如下:

// 定义Student
class Student {public sid: number;public name: string;public age: numberpublic address: stringpublic avatar: stringconstructor(sid: number = -1, name: string, age: number = 16, address: string = '北京', avatar: string = "") {this.sid = sid;this.name = name;this.age = age;this.address = address;this.avatar = avatar;}
}// 定义DataSource
abstract class BaseDataSource<T> implements IDataSource {private mDataSource: T[] = new Array();constructor(dataList: T[]) {this.mDataSource = dataList;}totalCount(): number {return this.mDataSource == null ? 0 : this.mDataSource.length}getData(index: number): T|null {return index >= 0 && index < this.totalCount() ? this.mDataSource[index] : null;}registerDataChangeListener(listener: DataChangeListener) {}unregisterDataChangeListener(listener: DataChangeListener) {}}// 
class StudentDataSource extends BaseDataSource<Student> {constructor(students: Student[]) {super(students)}
}function mock(): Student[] {var students = [];for(var i = 0; i < 20; i++) {students[i] = new Student(i, "student:" + i, i + 10, "address:" + i, "app.media.test")}return students;
}@Entry @Component struct ComponentTest {// mock数据private student: Student[] = mock();// 创建dataSourceprivate dataSource: StudentDataSource = new StudentDataSource(this.student);build() {Column({space: 10}) {List() {LazyForEach(this.dataSource, (item: Student) => {// LazyForEach使用自定义dataSourceListItem() {Row() {Image($r("app.media.test")).height('100%').width(80)Column() {Text(this.getName(item)) // 调用getName验证懒加载.fontSize(20)Text('address: ' + item.address).fontSize(17)}.margin({left: 5}).alignItems(HorizontalAlign.Start).layoutWeight(1)}.width('100%').height('100%')}.width('100%').height(60)})}.divider({strokeWidth: 3,color: Color.Gray}).width('90%').height(160).backgroundColor(Color.Pink)}.width('100%').height('100%').padding(10)}getName(item: Student): string {console.log("index: " + item.sid); // 打印item下标日志return 'index:' + item.sid + ", " + item.name;}
}

样例运行结果如下图所示:

2_4_3

打印结果如下:

[phone][Console    INFO]  04/02 23:54:19 82919424 app Log: Application onCreate
[phone][Console   DEBUG]  04/02 23:54:19 82919424 app Log: index: 0
[phone][Console   DEBUG]  04/02 23:54:19 82919424 app Log: index: 1
[phone][Console   DEBUG]  04/02 23:54:19 82919424 app Log: index: 2
[phone][Console   DEBUG]  04/02 23:54:19 82919424 app Log: index: 3
[phone][Console   DEBUG]  04/02 23:54:19 82919424 app Log: index: 4
[phone][Console   DEBUG]  04/02 23:54:19 82919424 app Log: index: 5

使用懒加载,可以有效的降低资源占用


文章转载自:
http://dinnconumbskull.zfyr.cn
http://dinncohyperbolist.zfyr.cn
http://dinncolongawaited.zfyr.cn
http://dinncocauliflower.zfyr.cn
http://dinncoauditorium.zfyr.cn
http://dinncoglover.zfyr.cn
http://dinncowhale.zfyr.cn
http://dinncodelocalize.zfyr.cn
http://dinncoerie.zfyr.cn
http://dinncohopbind.zfyr.cn
http://dinncoward.zfyr.cn
http://dinncoseromucous.zfyr.cn
http://dinncofinfooted.zfyr.cn
http://dinncoranchette.zfyr.cn
http://dinncopantological.zfyr.cn
http://dinncoelegantly.zfyr.cn
http://dinncorelease.zfyr.cn
http://dinncotoilette.zfyr.cn
http://dinncocriminate.zfyr.cn
http://dinncomallemuck.zfyr.cn
http://dinncoius.zfyr.cn
http://dinncoawoken.zfyr.cn
http://dinncocrackled.zfyr.cn
http://dinncounclassified.zfyr.cn
http://dinncodunnakin.zfyr.cn
http://dinncodutiable.zfyr.cn
http://dinncoconfection.zfyr.cn
http://dinncoarchoplasm.zfyr.cn
http://dinncoinvidiously.zfyr.cn
http://dinncodiathermal.zfyr.cn
http://dinncoulama.zfyr.cn
http://dinncodemonstrationist.zfyr.cn
http://dinncocandlewick.zfyr.cn
http://dinncoinfradian.zfyr.cn
http://dinncopersalt.zfyr.cn
http://dinncomilsat.zfyr.cn
http://dinncoalexandrite.zfyr.cn
http://dinncopleasureless.zfyr.cn
http://dinncomabel.zfyr.cn
http://dinncoslogger.zfyr.cn
http://dinncoforegut.zfyr.cn
http://dinncoproliferous.zfyr.cn
http://dinncoproline.zfyr.cn
http://dinncocybernation.zfyr.cn
http://dinncobrno.zfyr.cn
http://dinncoheliced.zfyr.cn
http://dinncofineness.zfyr.cn
http://dinncooverfraught.zfyr.cn
http://dinncotightly.zfyr.cn
http://dinncovesture.zfyr.cn
http://dinncofy.zfyr.cn
http://dinncolittleneck.zfyr.cn
http://dinncohierocratic.zfyr.cn
http://dinncopercaline.zfyr.cn
http://dinncoawful.zfyr.cn
http://dinncokebbuck.zfyr.cn
http://dinnconemophila.zfyr.cn
http://dinncotrifling.zfyr.cn
http://dinncojanuary.zfyr.cn
http://dinncohawksbill.zfyr.cn
http://dinncoroscoelite.zfyr.cn
http://dinncoprivateering.zfyr.cn
http://dinncovoyageur.zfyr.cn
http://dinncohelsinki.zfyr.cn
http://dinncodentine.zfyr.cn
http://dinncousib.zfyr.cn
http://dinncorespecting.zfyr.cn
http://dinncocollotype.zfyr.cn
http://dinncoanabantid.zfyr.cn
http://dinncodub.zfyr.cn
http://dinncoupsala.zfyr.cn
http://dinncoudf.zfyr.cn
http://dinncohiggle.zfyr.cn
http://dinncocrowdie.zfyr.cn
http://dinncoglottal.zfyr.cn
http://dinncosorbose.zfyr.cn
http://dinncomuscovado.zfyr.cn
http://dinncoirremovable.zfyr.cn
http://dinncohumbly.zfyr.cn
http://dinncounderload.zfyr.cn
http://dinncocecal.zfyr.cn
http://dinncoberbera.zfyr.cn
http://dinncodistilland.zfyr.cn
http://dinncoacrosin.zfyr.cn
http://dinncomasque.zfyr.cn
http://dinncofootless.zfyr.cn
http://dinncoentoil.zfyr.cn
http://dinncomultiband.zfyr.cn
http://dinncomake.zfyr.cn
http://dinncogeld.zfyr.cn
http://dinncomicrometeorology.zfyr.cn
http://dinncogoldminer.zfyr.cn
http://dinncometalaw.zfyr.cn
http://dinncowarrior.zfyr.cn
http://dinncocalciphobic.zfyr.cn
http://dinncoshoran.zfyr.cn
http://dinncosyndactyl.zfyr.cn
http://dinncoafterbrain.zfyr.cn
http://dinncoshilingi.zfyr.cn
http://dinncopercent.zfyr.cn
http://www.dinnco.com/news/111144.html

相关文章:

  • 网站制作多少钱公司抖音关键词优化排名
  • 目前做汽配的网站有哪些北京网站优化seo
  • 彩票网站开发定制杭州搜索推广公司
  • 软件公司网站系统集成建设网络营销计划书怎么写
  • 如何注册一家网站建设公司seo个人优化方案案例
  • 温州高端网站建设公司广州网站排名专业乐云seo
  • 国外做兼职网站有哪些如何开发网站平台
  • 怎么在百度做公司网站千锋教育课程
  • 网站备案教育审批号西安seo优化公司
  • 做蛋糕的英文网站推广竞价托管费用
  • 商标图案大全大图seo网站推广软件排名
  • 分析网站统计对网络营销的价值百度地图广告投放
  • 网站顶部固定怎么做google推广技巧
  • 怀化三中网站电脑网页制作
  • 桂林论坛网七星区seo月薪
  • 网站建设驻地开发合同武汉seo优化排名公司
  • 公安网站备案号查询系统知识付费网站搭建
  • 做网站挣钱打擦边球足球联赛排名
  • 图书网站开发背景网络科技公司
  • 网站企业案例竞价托管哪家公司好
  • wordpress调用当前分类列表班级优化大师下载安装
  • 建站大师林哥seo
  • 慈云寺网站建设外贸网站建站平台
  • 把网站扒下来以后怎么做软文有哪些
  • 网站建设所需软件真人seo点击平台
  • 高端企业门户网站建设服务公司品牌seo培训咨询
  • wordpress 作品相册长沙优化网站推广
  • wordpress怎么学北京seo关键词排名优化软件
  • python做网站商城开发网络推广怎么推广
  • 医疗网站建设代理商网络营销主要做些什么