目录

如何使用template设置多个模板并解决渲染异常问题

问题现象

使用Repeat渲染长列表时,当列表项存在多种不同模板类型时,通常会使用渲染模板(template)进行处理。一般有下面几种常见的问题场景:

背景知识

解决方案

场景一:模板数量多的情况。

  1. Repeat的渲染模板(template)能力,需要通过.templateId()方法为每个数据项指定模板类型,并在.template()中定义对应的渲染逻辑。不同模板类型对应不同的子组件结构,Repeat会根据类型自动复用相同模板的组件节点。详细参考:循环渲染能力说明。
  2. 若模板数量多时,不希望重复设置template定义多个模板,避免代码重复。可以考虑把结构相似的子组件放在一个template中处理,将数据项相关信息(例如item,index等)作为参数传给@Builder函数,在@Builder内使用visibility、if/else条件渲染等方式进行处理。例如通过visibility控制子组件内部组件的显示隐藏:
    @Entry
    @ComponentV2
    struct TemplatePageOne {
      @Local message: string[] = ['1', '2', '1', '2', '3', '3', '4', '4', '5', '5',];
    
      build() {
        Column() {
          List({ space: 18 }) {
            Repeat<string>(this.message)
              .virtualScroll() // 开启虚拟滚动
              .each((item: RepeatItem<string>) => {
                ListItem() {
                  Text(`each: ${item.item}`)
                    .backgroundColor('#f1f3f5')
                    .margin({
                      left: 16,
                      right: 16
                    });
                };
              })
              .key((item: string, index: number) => {
                return item + '_' + index;
              })
              .templateId((item: string, index: number): string => {
                console.info(`item = ${item}, index = ${index}`);
                if (item == '3' || item == '4') {
                  return 'other';
                }
                return item;
              })
                // 由template type渲染对应的template子组件
              .template('1', (item: RepeatItem<string>) => {
                ListItem() {
                  Text(`template_1: ${item.item}`)
                    .backgroundColor('#f1f3f5')
                    .margin({
                      left: 16,
                      right: 16
                    });
                };
              }, { cachedCount: 2 })
              .template('2', (item: RepeatItem<string>) => {
                ListItem() {
                  Text(`template_2: ${item.item}`)
                    .backgroundColor('#f1f3f5')
                    .margin({
                      left: 16,
                      right: 16
                    });
                };
              }, { cachedCount: 2 })
              .template('other', (item: RepeatItem<string>) => {
                ListItem() {
                  Row() {
                    Text(`template_other: `)
                      .backgroundColor('#f1f3f5')
                      .margin({
                        left: 16,
                        right: 16
                      });
                    // 通过visibility控制组件显示隐藏
                    Text(`this is template 3`)
                      .backgroundColor('#f1f3f5')
                      .visibility(item.item == '3' ? Visibility.Visible : Visibility.None)
                      .margin({
                        left: 16,
                        right: 16
                      });
                    Text(`this is template 4`)
                      .backgroundColor('#f1f3f5')
                      .visibility(item.item == '4' ? Visibility.Visible : Visibility.None)
                      .margin({
                        left: 16,
                        right: 16
                      });
                  }
                };
              }, { cachedCount: 2 });
    
          }
          .layoutWeight(1)
          .width('100%');
        };
      }
    }

场景二:刷新数据源,数据项未按照设定模板进行渲染。

  1. template渲染模板能力需要启用虚拟滚动(virtualScroll)。
  2. 当未开启virtualScroll()时,Repeat会一次性渲染全部数据项对应的组件,template的复用失效。详细参考:关闭懒加载。
  3. 所以刷新数据源后,虽然template type变化,但是template的复用失效,仍显示首次渲染的模板。需要手动设置.virtualScroll(),完整示例如下:
    @Entry
    @ComponentV2
    struct TemplatePageTwo {
      @Local message: string[] = ['1', '1', '1', '1', '1', '1', '1', '1'];
    
      build() {
        Column() {
          // 刷新数据源
          Button('refresh')
            .onClick(() => {
              this.message = ['2', '3', '2', '3', '2', '3', '2', '2', '3', '2', '3', '2', '3', '2'];
            })
            .margin({
              left: 16,
              top: 16,
              bottom: 16,
              right: 16
            });
    
          List({ space: 18 }) {
            Repeat<string>(this.message)
              .virtualScroll() // 开启虚拟滚动
              .each((item: RepeatItem<string>) => {
                ListItem() {
                  Text(`each: ${item.item}`)
                    .backgroundColor('#f1f3f5')
                    .margin({
                      left: 16,
                      right: 16
                    });
                };
              })
              .key((item: string, index: number) => {
                return item + '_' + index;
              })
              .templateId((item: string, index: number): string => {
                console.info(`item = ${item}, index = ${index}`);
                return item;
              })
                // 由template type渲染对应的template子组件
              .template('1', (item: RepeatItem<string>) => {
                ListItem() {
                  Text(`template_1: ${item.item}`)
                    .backgroundColor('#f1f3f5')
                    .margin({
                      left: 16,
                      right: 16
                    });
                };
              }, { cachedCount: 2 })
              .template('2', (item: RepeatItem<string>) => {
                ListItem() {
                  Text(`template_2: ${item.item}`)
                    .backgroundColor('#f1f3f5')
                    .margin({
                      left: 16,
                      right: 16
                    });
                };
              }, { cachedCount: 2 });
          }
          .layoutWeight(1)
          .width('100%');
        };
      }
    }