使用Repeat渲染长列表时,当列表项存在多种不同模板类型时,通常会使用渲染模板(template)进行处理。一般有下面几种常见的问题场景:
是否需要对每个模板都单独设置template,对于模板多的情况是否有其他方式减少重复设置template,例如下面场景:
// ... Repeat<string>(this.message) .virtualScroll() // 开启虚拟滚动 .each((item: RepeatItem<string>) => { // ... }) .key((item: string, index: number) => { return item + '_' + index; }) .templateId((item: string, index: number): string => { return item; }) // 由template type渲染对应的template子组件 .template('1', (item: RepeatItem<string>) => { // 模板1 }) .template('2', (item: RepeatItem<string>) => { // 模板2 }) .template('3', (item: RepeatItem<string>) => { // 模板3 }) .template('4', (item: RepeatItem<string>) => { // 模板4 }) .template('5', (item: RepeatItem<string>) => { // 模板5 }) // ... .template('100', (item: RepeatItem<string>) => { // 模板100 });
首次渲染时,按照template type渲染对应的template子组件,但刷新数据源后,数据项的template type变化,对应的数据项未按照对应的template进行渲染,仍显示首次渲染的模板,问题代码如下:
@Entry @ComponentV2 struct RepeatPage { @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) .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%'); }; } }
场景一:模板数量多的情况。
@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%'); }; } }
场景二:刷新数据源,数据项未按照设定模板进行渲染。
@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%'); }; } }