鸿蒙列表新的实现方式

文章目录

  • 前言
  • Repeat的介绍
  • 效果截图
  • 总结

一、前言

记得拉倒底看效果图。

鸿蒙的更新还是很快的,一不注意,就有性能提升的组件或者api 出现。

从一开始api9,一直到现在,其实经历了很多,比如List是最明显的,这个组件是用来列表展示的,最开始使用的是ForEach来循环子组件,接着如果是长列表为了更好的性能,就需要使用LazyForEach,不过它有点难用,直到后面又出现了新的 Repeat。

所以呢,如果是新开发的页面,如果是列表,直接用Repeat,使用起来也非常简单。

这就好比,以前我们在android开发的时候,使用ListView 和 使用 Recyclerview的感觉。当有了RecyclerView,可以直接放弃ListView,因为RecyclerView 性能更好,而且覆盖了ListView的功能。

同理,鸿蒙有了Repeat,新开发的直接用List + Repeat 即可。

最后呢,我会把代码附上。另外最后有页面的效果截图,看完再看代码,这样会更加直观。

二、简介

Repeat 主要是为了提升开发效率,简化重复性UI逻辑,并优化动态列表性能。

1、简化重复UI的构建

通过数据源直接生成重复的UI布局

cpp 复制代码
     Repeat<number>(this.mNumberLists)
          .each((item: RepeatItem<number>)=>{//组件生成函数,为每一个数据生成一个Text组件。
            ListItem(){
              // 默认布局
                Text('' + item.item)
                  .textAlign(TextAlign.Center)
                  .width('90%')
                  .height(72)
                  .backgroundColor('#FFFFFF')
                  .borderRadius(24)
            }

          })
          .key((item: number)=> item.toString())
2、模板使用

根据templateId 返回值,在 template 中作区分,显示不同的模板

cpp 复制代码
//不同的模板id
 .templateId((item: number, index: number)=>{
            if (index < 3) {
              return 'templeA'
            }else if (index < 10){
              return 'templeB'
            }else{
              return ''
            }
          })
          
   //不同的模板       
  .template('templeA', (ri: RepeatItem<number>)=>{
            ListItem(){
              Text('我是A模板' + ri.item)
                .textAlign(TextAlign.Center)
                .width('90%')
                .height(72)
                .backgroundColor('#FFFFFF')
                .borderRadius(24)
            }

          })
          .template('templeB', (ri: RepeatItem<number>)=>{
            ListItem(){
              Text('我是B模板' + ri.item)
                .textAlign(TextAlign.Center)
                .width('90%')
                .height(72)
                .backgroundColor(Color.Blue)
                .borderRadius(24)
            }
          })
3、条件渲染

通过if/else 在each中可显示不同模板,实现条件化渲染

cpp 复制代码
   .each((item: RepeatItem<number>)=>{
          ListItem(){
              //条件渲染,不同的条件显示不同的布局
              if (item.index % 2 === 0) {
                Text('' + item.item)
                  .textAlign(TextAlign.Center)
                  .width('90%')
                  .height(72)
                  .backgroundColor('#FFFFFF')
                  .borderRadius(24)
              }else{
                Text('我是奇数布局' + item.item)
                  .textAlign(TextAlign.Center)
                  .width('90%')
                  .height(72)
                  .backgroundColor('#FFFFFF')
                  .borderRadius(24)
              }

            }

          })
4、Repeat 性能上的优化
cpp 复制代码
1、key函数确保列表高效复用
2、virtualScroll 启用懒加载,提升长列表性能
3、模板复用
5、注意事项

使用Repeat 确保是在V2中使用,避免出现问题

三、效果截图

以上全部代码

cpp 复制代码
@Entry
@ComponentV2
struct RepeatTestPage{

  @Local mNumberLists: Array<number> = new Array()
  aboutToAppear(): void {
    for (let i = 0; i < 50; i++) {
      this.mNumberLists.push(i)
    }
  }

  build() {
    Column(){
      List({space: 12}){
        Repeat<number>(this.mNumberLists)
          .each((item: RepeatItem<number>)=>{//组件生成函数,为每一个数据生成一个Text组件。(默认渲染的模板)
            ListItem(){
              //条件渲染,不同的条件显示不同的布局
              // if (item.index % 2 === 0) {
              //   Text('' + item.item)
              //     .textAlign(TextAlign.Center)
              //     .width('90%')
              //     .height(72)
              //     .backgroundColor('#FFFFFF')
              //     .borderRadius(24)
              // }else{
              //   Text('我是奇数布局' + item.item)
              //     .textAlign(TextAlign.Center)
              //     .width('90%')
              //     .height(72)
              //     .backgroundColor('#FFFFFF')
              //     .borderRadius(24)
              // }

              // 默认布局
                Text('' + item.item)
                  .textAlign(TextAlign.Center)
                  .width('90%')
                  .height(72)
                  .backgroundColor('#FFFFFF')
                  .borderRadius(24)

            }

          })
          .key((item: number)=> item.toString())//指定key,避免ui渲染问题
          .virtualScroll({totalCount: this.mNumberLists.length})//打开懒加载,totalCount为预期的懒加载数据长度
          .templateId((item: number, index: number)=>{
            if (index < 3) {
              return 'templeA'
            }else if (index < 10){
              return 'templeB'
            }else{
              return ''
            }
          })
          .template('templeA', (ri: RepeatItem<number>)=>{
            ListItem(){
              Text('我是A模板' + ri.item)
                .textAlign(TextAlign.Center)
                .width('90%')
                .height(72)
                .backgroundColor('#FFFFFF')
                .borderRadius(24)
            }

          })
          .template('templeB', (ri: RepeatItem<number>)=>{
            ListItem(){
              Text('我是B模板' + ri.item)
                .textAlign(TextAlign.Center)
                .width('90%')
                .height(72)
                .backgroundColor(Color.Blue)
                .borderRadius(24)
            }
          })


      }
      .alignListItem(ListItemAlign.Center)
      .cachedCount(2)//容器组件的预加载区域大小


    }
    .width('100%')
    .height('100%')
    .backgroundColor('#F1F3F5')
  }

}

总结

1、Repeat 简介

2、Repeat 使用

3、效果截图

如果对你有一点点帮助,那是值得高兴的事情。:)

我的csdn:blog.csdn.net/shenshizhon...

我的简书:www.jianshu.com/u/345daf021...

相关推荐
再见6586 小时前
【HarmonyOS】 Todo 应用开发实战
harmonyos
爱吃大芒果7 小时前
面向大型鸿蒙原生应用的工程基建:核心路由、全局样式库与状态管理设计图纸
华为·harmonyos
轻口味11 小时前
HarmonyOS 6.1.1 全栈实战录 - 91 实战 Call Service Kit 扩展企服来去电智慧
华为·harmonyos·鸿蒙
木斯佳12 小时前
鸿蒙开发入门指南:前端开发者快速理解视频编码概念——输入模式
华为·音视频·harmonyos
不羁的木木13 小时前
《HarmonyOS技术精讲》二:用户动作与状态感知实战
华为·harmonyos
G_dou_16 小时前
Flutter+OpenHarmony 实战:stopwatch 秒表应用
flutter·harmonyos
亚信安全官方账号17 小时前
AISTrustOne鸿蒙版安全方案 让终端防护“内生”力量觉醒
安全·华为·harmonyos
夜勤月17 小时前
HarmonyOS 6.0 ArkWeb实战:PDF背景色自定义功能全解析(附完整代码+避坑指南)
华为·pdf·harmonyos
想你依然心痛18 小时前
HarmonyOS 6(API 23)实战:基于悬浮导航、沉浸光感与HMAF的“药界智脑“——PC端AI智能体沉浸式药物研发与分子模拟工作台
人工智能·华为·ar·harmonyos·智能体
G_dou_19 小时前
Flutter +OpenHarmony 实战:clock 时钟应用
flutter·harmonyos