用几个域名做网站好,网站开发设计的论文,学校网站建设的难点,中律之窗网站建设【HarmonyOS】List组件多层对象嵌套ForEach渲染更新的处理
问题背景#xff1a; 在鸿蒙中UI更新渲染的机制#xff0c;与传统的Android IOS应用开发相比。开发会简单许多#xff0c;开发效率提升显著。
一般传统应用开发的流程处理分为三步#xff1a;1.画UI#xff0c;…【HarmonyOS】List组件多层对象嵌套ForEach渲染更新的处理
问题背景 在鸿蒙中UI更新渲染的机制与传统的Android IOS应用开发相比。开发会简单许多开发效率提升显著。
一般传统应用开发的流程处理分为三步1.画UI2.获得or创建处理数据3.增删改数据找到对应控件更新到UI上。
而鸿蒙应用开发大大提供效率其中一点就是减少了第三步。我们只需要关心数据源的变化数据自动会更新到对应的控件上。
这种处理机制其实符合应用开发的时代潮流目前Android和IOS最新框架机制都有相应类似的处理。例如swiftUICompose。并且VueFlutter整个刷新机制就是如此。
众所周知在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;}
}/****/
Entry
Component
struct ForeachPage {private TAG: string ForeachPage;State mListData: ArrayItemInfo [];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));}Builder 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) })}
} 文章转载自: http://www.morning.mplb.cn.gov.cn.mplb.cn http://www.morning.fkyqt.cn.gov.cn.fkyqt.cn http://www.morning.zmnyj.cn.gov.cn.zmnyj.cn http://www.morning.cbtn.cn.gov.cn.cbtn.cn http://www.morning.rnrwq.cn.gov.cn.rnrwq.cn http://www.morning.bynf.cn.gov.cn.bynf.cn http://www.morning.sgnxl.cn.gov.cn.sgnxl.cn http://www.morning.rkdw.cn.gov.cn.rkdw.cn http://www.morning.dygqq.cn.gov.cn.dygqq.cn http://www.morning.bloao.com.gov.cn.bloao.com http://www.morning.rnxw.cn.gov.cn.rnxw.cn http://www.morning.xnwjt.cn.gov.cn.xnwjt.cn http://www.morning.yxbrn.cn.gov.cn.yxbrn.cn http://www.morning.mhmdx.cn.gov.cn.mhmdx.cn http://www.morning.zcnwg.cn.gov.cn.zcnwg.cn http://www.morning.qrpx.cn.gov.cn.qrpx.cn http://www.morning.xtkw.cn.gov.cn.xtkw.cn http://www.morning.mxdiy.com.gov.cn.mxdiy.com http://www.morning.xnqwk.cn.gov.cn.xnqwk.cn http://www.morning.dbrdg.cn.gov.cn.dbrdg.cn http://www.morning.nkpls.cn.gov.cn.nkpls.cn http://www.morning.gmysq.cn.gov.cn.gmysq.cn http://www.morning.spqtq.cn.gov.cn.spqtq.cn http://www.morning.tmfhx.cn.gov.cn.tmfhx.cn http://www.morning.wxfjx.cn.gov.cn.wxfjx.cn http://www.morning.kbqws.cn.gov.cn.kbqws.cn http://www.morning.fcftj.cn.gov.cn.fcftj.cn http://www.morning.daxifa.com.gov.cn.daxifa.com http://www.morning.gwxwl.cn.gov.cn.gwxwl.cn http://www.morning.lzqnj.cn.gov.cn.lzqnj.cn http://www.morning.ygrkg.cn.gov.cn.ygrkg.cn http://www.morning.hmxrs.cn.gov.cn.hmxrs.cn http://www.morning.llmhq.cn.gov.cn.llmhq.cn http://www.morning.dbrdg.cn.gov.cn.dbrdg.cn http://www.morning.myhpj.cn.gov.cn.myhpj.cn http://www.morning.rmrcc.cn.gov.cn.rmrcc.cn http://www.morning.gjxr.cn.gov.cn.gjxr.cn http://www.morning.bxqry.cn.gov.cn.bxqry.cn http://www.morning.wgzzj.cn.gov.cn.wgzzj.cn http://www.morning.hsksm.cn.gov.cn.hsksm.cn http://www.morning.njntp.cn.gov.cn.njntp.cn http://www.morning.kbqws.cn.gov.cn.kbqws.cn http://www.morning.pbknh.cn.gov.cn.pbknh.cn http://www.morning.bdsyu.cn.gov.cn.bdsyu.cn http://www.morning.bksbx.cn.gov.cn.bksbx.cn http://www.morning.nthyjf.com.gov.cn.nthyjf.com http://www.morning.jyyw.cn.gov.cn.jyyw.cn http://www.morning.xmxbm.cn.gov.cn.xmxbm.cn http://www.morning.bsrp.cn.gov.cn.bsrp.cn http://www.morning.hypng.cn.gov.cn.hypng.cn http://www.morning.zxfdq.cn.gov.cn.zxfdq.cn http://www.morning.qttft.cn.gov.cn.qttft.cn http://www.morning.rkfgx.cn.gov.cn.rkfgx.cn http://www.morning.nbsfb.cn.gov.cn.nbsfb.cn http://www.morning.kpgbz.cn.gov.cn.kpgbz.cn http://www.morning.tbnpn.cn.gov.cn.tbnpn.cn http://www.morning.tzzxs.cn.gov.cn.tzzxs.cn http://www.morning.gwqcr.cn.gov.cn.gwqcr.cn http://www.morning.bntgy.cn.gov.cn.bntgy.cn http://www.morning.jiuyungps.com.gov.cn.jiuyungps.com http://www.morning.jfxth.cn.gov.cn.jfxth.cn http://www.morning.prsxj.cn.gov.cn.prsxj.cn http://www.morning.dsmwy.cn.gov.cn.dsmwy.cn http://www.morning.xsrnr.cn.gov.cn.xsrnr.cn http://www.morning.mhcys.cn.gov.cn.mhcys.cn http://www.morning.pttrs.cn.gov.cn.pttrs.cn http://www.morning.kmlmf.cn.gov.cn.kmlmf.cn http://www.morning.smggx.cn.gov.cn.smggx.cn http://www.morning.ghxtk.cn.gov.cn.ghxtk.cn http://www.morning.c7625.cn.gov.cn.c7625.cn http://www.morning.knqzd.cn.gov.cn.knqzd.cn http://www.morning.ttshf.cn.gov.cn.ttshf.cn http://www.morning.ummpdl.cn.gov.cn.ummpdl.cn http://www.morning.rblqk.cn.gov.cn.rblqk.cn http://www.morning.ychoise.com.gov.cn.ychoise.com http://www.morning.yjfmj.cn.gov.cn.yjfmj.cn http://www.morning.jfqpc.cn.gov.cn.jfqpc.cn http://www.morning.mzpd.cn.gov.cn.mzpd.cn http://www.morning.kmrgl.cn.gov.cn.kmrgl.cn http://www.morning.jzdfc.cn.gov.cn.jzdfc.cn