在List、Grid等存在大量子组件的场景下,常使用LazyForEach进行数据懒加载渲染。但通常因使用错误,遇到UI刷新异常的问题,针对UI刷新异常,可以考虑从键值方面进行排查。
列表滑动时显示的列表项顺序混乱,数据显示重复。问题代码如下:
// 代码中引入的MyDataSource在解决方案中说明
import { MyDataSource } from '../common/data';
class Product {
money: number = 0;
text: string = '';
title: string = '';
data: string = '';
constructor(money: number, text: string, title: string, data: string) {
this.money = money;
this.text = text;
this.title = title;
this.data = data;
}
}
@Entry
@Component
struct KeyDemoOne {
private data: MyDataSource<Product> = new MyDataSource<Product>();
aboutToAppear(): void {
// 请求数据模拟
for (let i = 0; i < 100; i++) {
let str: string = (i % 2 == 0) ? '限部分商品使用' : '全场可用';
let product: Product = new Product(i, `满${i * 10}元立减`, str, `${i}天内有效`);
this.data.pushData(product);
}
}
build() {
Column() {
List({ space: 10 }) {
LazyForEach(this.data, (item: Product) => {
ListItem() {
Row() {
Column() {
Text(item.money.toString())
.margin({ top: 20, bottom: 20 })
.fontColor('#f9f4d7')
.borderRadius(5);
Text(item.text)
.textAlign(TextAlign.Center)
.fontSize(12)
.fontColor('#f9f4d7')
.borderRadius(5);
}
.width('30%')
.height(100)
.backgroundColor('#fe6a48');
Column() {
Row() {
Text(item.title)
.textAlign(TextAlign.Start)
.margin({ top: 15, left: -110, bottom: 40 });
};
Row() {
Text(item.data)
.fontSize(10)
.margin({ left: -5, right: 25 });
};
}
.width('70%')
.height(100)
.backgroundColor('#f1f2f3');
};
};
}, (item: Product) => item.title);
};
}
.width('100%')
.height('100%')
.backgroundColor('#ffffff');
}
} 修改数据源中的数据项后,数据的修改在UI中未刷新,显示的还是初始的数据。问题代码如下:
// 代码中引入的MyDataSource在解决方案中说明
import { MyDataSource } from '../common/data';
class Product {
money: number = 0;
text: string = '';
title: string = '';
data: string = '';
id: string = '';
constructor(money: number, text: string, title: string, data: string, id: string) {
this.money = money;
this.text = text;
this.title = title;
this.data = data;
this.id = id;
}
}
@Entry
@Component
struct KeyDemoTwo {
private data: MyDataSource<Product> = new MyDataSource<Product>();
aboutToAppear(): void {
// 请求数据模拟
for (let i = 0; i < 100; i++) {
let id = i.toString(); // 设置一个唯一的id,实际业务中可以使用其他唯一值作为键值
let str: string = (i % 2 == 0) ? '限部分商品使用' : '全场可用';
let product: Product = new Product(i, `满${i * 10}元立减`, str, `${i}天内有效`, id);
this.data.pushData(product);
}
}
build() {
Column() {
Button('修改数据')
.onClick(() => {
let index = 3;
let product: Product = this.data.getData(index);
product.title = '已使用';
this.data.setData(product, index);
})
.margin({ top: 16, bottom: 16 });
List({ space: 10 }) {
LazyForEach(this.data, (item: Product) => {
ListItem() {
Row() {
Column() {
Text(item.money.toString())
.margin({ top: 20, bottom: 20 })
.fontColor('#f9f4d7')
.borderRadius(5);
Text(item.text)
.textAlign(TextAlign.Center)
.fontSize(12)
.fontColor('#f9f4d7')
.borderRadius(5);
}
.width('30%')
.height(100)
.backgroundColor('#fe6a48');
Column() {
Row() {
Text(item.title)
.textAlign(TextAlign.Start)
.margin({ top: 15, left: -110, bottom: 40 });
};
Row() {
Text(item.data)
.fontSize(10)
.margin({ left: -5, right: 25 });
};
}
.width('70%')
.height(100)
.backgroundColor('#f1f2f3');
};
};
}, (item: Product) => item.id); // 使用唯一值作为键值
};
}
.width('100%')
.height('100%')
.backgroundColor('#ffffff');
}
} 对于LazyForEach使用过程中常见的UI刷新异常,可以考虑先从键值方面进行排查,主要排查:
场景一:在UI更新时,如果出现重复的键值,框架可能无法正常工作。在问题代码中,使用了title属性作为键值,键值不唯一导致数据渲染混乱。
场景二:键值变化时,ArkUI框架将视为该数组元素已被替换或修改,并基于新的键值创建新的组件。问题代码中修改的是title属性,但键值使用的是id,键值未变化导致组件不会重新渲染。
import { MyDataSource } from '../common/data';
class Product {
money: number = 0;
text: string = '';
title: string = '';
data: string = '';
id: string = '';
constructor(money: number, text: string, title: string, data: string, id: string) {
this.money = money;
this.text = text;
this.title = title;
this.data = data;
this.id = id;
}
}
@Entry
@Component
struct KeyDemoOne {
private data: MyDataSource<Product> = new MyDataSource<Product>();
aboutToAppear(): void {
// 请求数据模拟
for (let i = 0; i < 100; i++) {
let id = i.toString(); // 设置一个唯一的id,实际业务中可以使用其他唯一值作为键值
let str: string = (i % 2 == 0) ? '限部分商品使用' : '全场可用';
let product: Product = new Product(i, `满${i * 10}元立减`, str, `${i}天内有效`, id);
this.data.pushData(product);
}
}
build() {
Column() {
List({ space: 10 }) {
LazyForEach(this.data, (item: Product) => {
ListItem() {
Row() {
Column() {
Text(item.money.toString())
.margin({ top: 20, bottom: 20 })
.fontColor('#f9f4d7')
.borderRadius(5);
Text(item.text)
.textAlign(TextAlign.Center)
.fontSize(12)
.fontColor('#f9f4d7')
.borderRadius(5);
}
.width('30%')
.height(100)
.backgroundColor('#fe6a48');
Column() {
Row() {
Text(item.title)
.textAlign(TextAlign.Start)
.margin({ top: 15, left: -110, bottom: 40 });
};
Row() {
Text(item.data)
.fontSize(10)
.margin({ left: -5, right: 25 });
};
}
.width('70%')
.height(100)
.backgroundColor('#f1f2f3');
};
};
}, (item: Product) => item.id); // 使用唯一值作为键值
};
}
.width('100%')
.height('100%')
.backgroundColor('#ffffff');
}
} 方案:针对问题场景,可以考虑将修改的属性作为键值的一部分,实际业务中可以根据情况设置合理的键值。
import { MyDataSource } from '../common/data';
class Product {
money: number = 0;
text: string = '';
title: string = '';
data: string = '';
id: string = '';
constructor(money: number, text: string, title: string, data: string, id: string) {
this.money = money;
this.text = text;
this.title = title;
this.data = data;
this.id = id;
}
}
@Entry
@Component
struct KeyDemoTwo {
private data: MyDataSource<Product> = new MyDataSource<Product>();
private counter: number = 0;
aboutToAppear(): void {
// 请求数据模拟
for (let i = 0; i < 100; i++) {
let id = i.toString(); // 设置一个唯一的id,实际业务中可以使用其他唯一值作为键值
let str: string = (i % 2 == 0) ? '限部分商品使用' : '全场可用';
let product: Product = new Product(i, `满${i * 10}元立减`, str, `${i}天内有效`, id);
this.data.pushData(product);
}
}
build() {
Column() {
Button('修改数据')
.onClick(() => {
let index = 3;
let product: Product = this.data.getData(index);
product.title = `已使用${++this.counter}次`;
this.data.setData(product, index);
})
.margin({ top: 16, bottom: 16 });
List({ space: 10 }) {
LazyForEach(this.data, (item: Product) => {
ListItem() {
Row() {
Column() {
Text(item.money.toString())
.margin({ top: 20, bottom: 20 })
.fontColor('#f9f4d7')
.borderRadius(5);
Text(item.text)
.textAlign(TextAlign.Center)
.fontSize(12)
.fontColor('#f9f4d7')
.borderRadius(5);
}
.width('30%')
.height(100)
.backgroundColor('#fe6a48');
Column() {
Row() {
Text(item.title)
.textAlign(TextAlign.Start)
.margin({ top: 15, left: -110, bottom: 40 });
};
Row() {
Text(item.data)
.fontSize(10)
.margin({ left: -5, right: 25 });
};
}
.width('70%')
.height(100)
.backgroundColor('#f1f2f3');
};
};
}, (item: Product) => item.id + item.title); // 可以根据实际业务设置,让键值随数据修改变化
};
}
.width('100%')
.height('90%')
.backgroundColor('#ffffff');
}
} 示例中使用的LazyForEach的数据源,根据实际业务需要在指定目录下定义(例如entry/src/main/ets/common/data.ets):
class BasicDataSource<T> implements IDataSource {
private listeners: DataChangeListener[] = [];
private originDataArray: T[] = [];
public totalCount(): number {
return this.originDataArray.length;
}
public getData(index: number): T {
return this.originDataArray[index];
}
registerDataChangeListener(listener: DataChangeListener): void {
if (this.listeners.indexOf(listener) < 0) {
console.info('add listener');
this.listeners.push(listener);
}
}
unregisterDataChangeListener(listener: DataChangeListener): void {
const pos = this.listeners.indexOf(listener);
if (pos >= 0) {
console.info('remove listener');
this.listeners.splice(pos, 1);
}
}
notifyDataReload(): void {
this.listeners.forEach(listener => {
listener.onDataReloaded();
});
}
notifyDataAdd(index: number): void {
this.listeners.forEach(listener => {
listener.onDataAdd(index);
});
}
notifyDataChange(index: number): void {
this.listeners.forEach(listener => {
listener.onDataChange(index);
});
}
notifyDataDelete(index: number): void {
this.listeners.forEach(listener => {
listener.onDataDelete(index);
});
}
notifyDataMove(from: number, to: number): void {
this.listeners.forEach(listener => {
listener.onDataMove(from, to);
});
}
notifyDatasetChange(operations: DataOperation[]): void {
this.listeners.forEach(listener => {
listener.onDatasetChange(operations);
});
}
}
// LazyForEach的数据源需要实现IDataSource接口
export class MyDataSource<T> extends BasicDataSource<T> {
private dataArray: T[] = [];
public setData(data: T, index: number): void {
if (this.dataArray.length > index) {
this.dataArray[index] = data;
this.notifyDataChange(index);
}
}
public getAllData(): T[] {
return this.dataArray;
}
public totalCount(): number {
return this.dataArray.length;
}
public getData(index: number): T {
return this.dataArray[index];
}
public pushData(data: T): void {
this.dataArray.push(data);
this.notifyDataAdd(this.dataArray.length - 1);
}
}