建议使用lazyforeach来加载长列表内容,可以规避该问题,参考代码如下:import { ImageKnifeComponent, ImageKnifeOption } from '@app/mpoinImageLoader'; import { MyData } from './MyData'; @Entry @Component struct Index { private dataSource: MyData = new MyData() option1:ImageKnifeOption = { loadSrc: 'xxxx', placeholderSrc: $r('app.media.app_icon'), } @State option2:ImageKnifeOption = { loadSrc: 'xxxx', placeholderSrc: $r('app.media.app_icon'), } private datas: ImageKnifeOption[] = [this.option1,this.option1,this.option1, this.option1, this.option1,this.option1,this.option1] aboutToAppear(): void { for (let i = 0; i <= 20; i++) { this.dataSource.pushData(this.option1) } } build() { LazyForEach(this.dataSource,(item: ImageKnifeOption, index: number) => { ListItem(){ ImageKnifeComponent({imageKnifeOption: item}).width(200).height(200) .margin({ top: 10, left: 20, right: 20 }) } }) } .width('100%') .height('100%') } } import { ImageKnifeOption } from '@app/mpoinImageLoader'; export class MyData implements IDataSource { private listeners: DataChangeListener[] = []; private datas: ImageKnifeOption[] = [] totalCount(): number { return this.datas.length } getData(index: number): ImageKnifeOption { return this.datas[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(); }) } // 通知LazyForEach组件需要在index对应索引处添加子组件 notifyDataAdd(index: number): void { this.listeners.forEach(listener => { listener.onDataAdd(index); }) } // 通知LazyForEach组件在index对应索引处数据有变化,需要重建该子组件 notifyDataChange(index: number): void { this.listeners.forEach(listener => { listener.onDataChange(index); }) } // 通知LazyForEach组件需要在index对应索引处删除该子组件 notifyDataDelete(index: number): void { this.listeners.forEach(listener => { listener.onDataDelete(index); }) } // 通知LazyForEach组件将from索引和to索引处的子组件进行交换 notifyDataMove(from: number, to: number): void { this.listeners.forEach(listener => { listener.onDataMove(from, to); }) } public addData(index: number, data: ImageKnifeOption): void { this.datas.splice(index, 0, data); this.notifyDataAdd(index); } public pushData(data: ImageKnifeOption): void { this.datas.push(data); this.notifyDataAdd(this.datas.length - 1); } }
建议使用lazyforeach来加载长列表内容,可以规避该问题,参考代码如下: