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

【HarmonyOS】List组件多层对象嵌套ForEach渲染更新的处理

【HarmonyOS】List组件多层对象嵌套ForEach渲染更新的处理

问题背景:
在鸿蒙中UI更新渲染的机制,与传统的Android IOS应用开发相比。开发会简单许多,开发效率提升显著。

一般传统应用开发的流程处理分为三步:1.画UI,2.获得or创建,处理数据,3.增删改数据,找到对应控件,更新到UI上。

而鸿蒙应用开发,大大提供效率其中一点,就是减少了第三步。我们只需要关心数据源的变化,数据自动会更新到对应的控件上。

这种处理机制,其实符合应用开发的时代潮流,目前Android和IOS最新框架机制都有相应类似的处理。例如swiftUI,Compose。并且Vue,Flutter整个刷新机制就是如此。

众所周知,在HarmonyOs的list组件渲染中,如果数据源列表对象是多个对象嵌套的处理,那最底层对象的属性更新时,UI界面是不会渲染的。因为检测不到,目前只能检测到第一层对象。

解决方案:

当然官方的api在持续迭代的过程中。对于冗余渲染,渲染的性能提升,还有多层对象嵌套的数据源更新问题,一直再迭代方法处理。

针对多层对象嵌套,底层对象属性修改后,UI不渲染的问题,有个比较简单又方便的处理方式,思路仅供参考。

即:深拷贝修改的item对象。

这样整个对象相当于都变化了,就符合第一层对象检测的机制,可以被系统捕获到刷新。

DEMO示例:

import { util } from '@kit.ArkTS';/*** 二级数据结构*/
class ChildInfo {index: number;constructor(index: number) {this.index = index;}
}/*** 一级数据结构*/
class ItemInfo {key: string = util.generateRandomUUID(true);name: string;icon: Resource;childInfo: ChildInfo;select: boolean;constructor(name: string, icon: Resource, index: number) {this.name = name;this.icon = icon;this.childInfo = new ChildInfo(index);this.select = false;}/*** 重新创建对象,深拷贝处理* @param itemInfo* @param index* @returns*/static deepCopy(itemInfo: ItemInfo, index: number){let info: ItemInfo = new ItemInfo(itemInfo.name, itemInfo.icon, index);info.select = itemInfo.select;info.key = itemInfo.key;info.childInfo = itemInfo.childInfo;return info;}
}/****/


struct ForeachPage {private TAG: string = "ForeachPage"; mListData: Array<ItemInfo> = [];aboutToAppear(): void {this.mListData.push(new ItemInfo('游戏', $r("app.media.iconA"), 1));this.mListData.push(new ItemInfo('游戏', $r("app.media.iconB"), 2));this.mListData.push(new ItemInfo('游戏', $r("app.media.iconA"), 3));this.mListData.push(new ItemInfo('游戏', $r("app.media.iconB"), 4));this.mListData.push(new ItemInfo('游戏', $r("app.media.iconA"), 5));this.mListData.push(new ItemInfo('游戏', $r("app.media.iconB"), 6));} ItemView(item: ItemInfo, index: number){Row() {Image(item.icon).width(px2vp(120)).height(px2vp(120))Text(item.name + "(" + item.childInfo.index + ")").fontSize(20)Blank()if(this.isLog(item, index)){if(item.select){Image($r("app.media.icon_check")).size({width: px2vp(72),height: px2vp(72)})}}}.width('100%').justifyContent(FlexAlign.Start).onClick(()=>{item.select = !item.select;if(item.select){item.childInfo.index = 666;}else{item.childInfo.index = index;}this.mListData[index] = ItemInfo.deepCopy(item, item.childInfo.index);console.log(this.TAG, " ItemView onClick: " + index + " item.select: " + item.select);})}private isLog(item: ItemInfo, index: number){console.log(this.TAG, " ItemView isLog index: " + index + " item.select: " + item.select);return true;}build() {List() {ForEach(this.mListData, (item: ItemInfo, index: number) => {ListItem() {this.ItemView(item, index)}}, (item: ItemInfo) => JSON.stringify(item))//}.width("100%").height("100%").padding({ left: px2vp(60), right: px2vp(60) })}
}

iconA
iconB
icon_check

相关文章:

  • 集成学习算法笔记
  • 已解决java.nio.charset.CoderMalfunctionError: 编码器故障错误的正确解决方法,亲测有效!!!
  • 有钱还系统源码,人人还众筹还钱模式还贷系统源码
  • SD-WAN简介
  • Android基础-UI布局
  • 正缘画像 api数据接口
  • 隐马尔可夫链
  • 【射击game】
  • Java筑基-面向对象
  • [SQL-SERVER:数据库安全及维护]:MSSM工具对用户进行用户授权和角色授权操作
  • 新书推荐:1.2 动态链接库与API
  • 961操作系统知识总结
  • 理解不同层的表示(layer representations)
  • AI图书推荐:结合ChatGPT的Java编程实用指南
  • 2024年BCSP-X小学高年级组初赛真题
  • (三)从jvm层面了解线程的启动和停止
  • Android Studio:GIT提交项目到远程仓库
  • Android框架之Volley
  • echarts花样作死的坑
  • HTTP中的ETag在移动客户端的应用
  • Intervention/image 图片处理扩展包的安装和使用
  • JavaScript 是如何工作的:WebRTC 和对等网络的机制!
  • Java比较器对数组,集合排序
  • k8s如何管理Pod
  • laravel with 查询列表限制条数
  • Less 日常用法
  • MYSQL 的 IF 函数
  • MYSQL如何对数据进行自动化升级--以如果某数据表存在并且某字段不存在时则执行更新操作为例...
  • python docx文档转html页面
  • Python实现BT种子转化为磁力链接【实战】
  • Vue 重置组件到初始状态
  • vue脚手架vue-cli
  • - 概述 - 《设计模式(极简c++版)》
  • 近期前端发展计划
  • 罗辑思维在全链路压测方面的实践和工作笔记
  • 浅谈JavaScript的面向对象和它的封装、继承、多态
  • 如何将自己的网站分享到QQ空间,微信,微博等等
  • 如何邀请好友注册您的网站(模拟百度网盘)
  • ​DB-Engines 12月数据库排名: PostgreSQL有望获得「2020年度数据库」荣誉?
  • ​LeetCode解法汇总2696. 删除子串后的字符串最小长度
  • #大学#套接字
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (2024.6.23)最新版MAVEN的安装和配置教程(超详细)
  • (21)起落架/可伸缩相机支架
  • (笔记)Kotlin——Android封装ViewBinding之二 优化
  • (翻译)terry crowley: 写给程序员
  • (理论篇)httpmoudle和httphandler一览
  • (七)Activiti-modeler中文支持
  • (十六)、把镜像推送到私有化 Docker 仓库
  • (一)appium-desktop定位元素原理
  • (原创)Stanford Machine Learning (by Andrew NG) --- (week 9) Anomaly DetectionRecommender Systems...
  • (转)机器学习的数学基础(1)--Dirichlet分布
  • (转)平衡树
  • .NET 6 Mysql Canal (CDC 增量同步,捕获变更数据) 案例版
  • .NET 8 中引入新的 IHostedLifecycleService 接口 实现定时任务