使用Repeat实现列表功能,加载新数据会出现重复显示问题,一般有下面几种问题场景:
常见问题场景 | 问题描述 |
|---|---|
场景一 | 滑动加载新数据,在aboutToAppear方法里处理数据逻辑,导致数据重复。 |
场景二 | Repeat与@Builder混用场景下,传参错误,导致数据重复。 |
根据问题代码分析,数据更新是在子组件的aboutToAppear方法里完成的,aboutToAppear在组件被创建时触发,之后不会再触发。因为Repeat提供了节点复用的能力,后续加载的是之前复用的节点,不会走aboutToAppear方法,导致数据没有更新。
问题代码如下:
@ComponentV2
struct LowCodeTitleView{
@Param ids: string = ''
@Local changes:string = ''
aboutToAppear(): void {
this.changes = this.ids // 数据更新是在子组件的aboutToAppear方法里完成的
}
build() {
Column(){
Text(this.changes+"标题内容")
}
}
}
@Entry
@ComponentV2
struct Index {
@Local dataArr: Array<string> = [];
aboutToAppear(): void {
for (let i = 0; i < 50; i++) {
this.dataArr.push(`data_${i}`); // 为数组添加一些数据
}
}
build() {
RelativeContainer() {
List({ space: 3 }) {
Repeat<string>(this.dataArr)
.each((ri: RepeatItem<string>) => {
ListItem() {
LowCodeTitleView({
ids:ri.item
})
}
})
.key((item: string, index: number): string => "__test_lowcode--"+index)
.virtualScroll({ totalCount: this.dataArr.length })
}.cachedCount(1)
}
.height('100%')
.width('100%')
}
} 问题代码如下:
List({ space: 10 }) {
Repeat<ItemDataV2>(this.imageList)
.each((ri: RepeatItem<ItemDataV2>) => {
ListItem() {
this.imageItemView(ri.item);
};
})
.key((item: ItemDataV2) => item.id.toString())
.templateId((item: ItemDataV2) => {
return item.type;
})
.virtualScroll({ totalCount: this.imageList.length });
} class Title {
id: number;
title: string;
constructor(id: number, title: string) {
this.id = id;
this.title = title;
}
}
@ComponentV2
struct LowCodeTitleView {
@Param title: Title = new Title(0, '标题内容'); // 数据从父组件传递给子组件,不走aboutToAppear
build() {
Column() {
Text(this.title.id + this.title.title);
}.margin({ left: 16, top: 10, bottom: 10 });
}
}
@Entry
@ComponentV2
struct RepeatLoadDataDemo {
@Local dataArr: Array<Title> = [];
aboutToAppear(): void {
for (let i = 0; i < 50; i++) {
this.dataArr.push(new Title(i, '标题内容')); // 为数组添加一些数据
}
}
build() {
RelativeContainer() {
List({ space: 3 }) {
Repeat<Title>(this.dataArr) // 把更新的数据放在数据源里
.each((ri: RepeatItem<Title>) => {
ListItem() {
LowCodeTitleView({
title: ri.item
});
};
})
.key((_item: Title, index: number): string => '__test_lowcode--' + index)
.virtualScroll({ totalCount: this.dataArr.length });
}.cachedCount(1).expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.BOTTOM]);
}.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.BOTTOM])
.height('100%')
.width('100%');
}
} 在API 18后,Repeat提供了关闭自身复用的能力,详细参考:VirtualScrollOptions;配合@ReusableV2装饰的组件使用,复用的时候也可以触发组件的aboutToReuse生命周期函数。
上述问题代码中this.imageItemView(ri.item)传参存在问题,必须将RepeatItem类型整体进行传参,应该改为this.imageItemView(ri)。
示例代码如下:
@ObservedV2
export class ItemDataV2 {
@Trace
id: number;
@Trace
type: string = '';
@Trace
title: ResourceStr;
@Trace
img: Resource;
constructor(id: number, title: ResourceStr, img: Resource, type?: string) {
this.id = id;
this.title = title;
this.img = img;
if (type) {
this.type = type;
}
}
}
@Entry
@ComponentV2
struct RepeatDemo {
@Local imageList: Array<ItemDataV2> = this.getFirstPageData();
// 模拟的第一页的数据
getFirstPageData(): Array<ItemDataV2> {
let imageList: Array<ItemDataV2> = [];
imageList.push(...getItemData(2, 10));
return imageList;
}
build() {
Column() {
Button('addItem').onClick(() => { // 点击按钮添加一页的数据
this.addTestData();
});
List({ space: 10 }) {
Repeat<ItemDataV2>(this.imageList)
.each((ri: RepeatItem<ItemDataV2>) => {
ListItem() {
this.imageItemView(ri);
};
})
.key((item: ItemDataV2) => item.id.toString())
.templateId((item: ItemDataV2) => {
return item.type;
})
.virtualScroll({ totalCount: this.imageList.length });
}.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.BOTTOM])
.cachedCount(6)
.width('100%')
.padding({
top: 15,
right: 15,
left: 15,
bottom: 12
});
}.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.BOTTOM]);
}
// 添加一页的数据
addTestData(): void {
let count = this.imageList.length;
let moreArr: ItemDataV2[] = getItemData(count, 10);
this.imageList.push(...moreArr);
}
@Builder
imageItemView(ri: RepeatItem<ItemDataV2>) {
Stack() {
Image(ri.item.img)
.objectFit(ImageFit.Cover)
.aspectRatio(3)
.borderRadius(12);
Text(ri.item.title)
.padding(15)
.fontSize(30)
.fontWeight(FontWeight.Bold)
.fontColor(Color.Black);
}.alignContent(Alignment.TopStart)
.width('100%');
}
}
let swiperImg: Array<Resource> = [$r('app.media.backImage')]; // 背景图
// 创建模拟的数据
function getItemData(start: number, count: number): ItemDataV2[] {
let arr: ItemDataV2[] = [];
for (let i = 0; i < count; i++) {
let imageIndex = i % swiperImg.length;
arr.push(new ItemDataV2(i + start, (i + start).toString(), swiperImg[imageIndex]));
}
return arr;
} 代码效果如下图:
场景一:

场景二:
