当网络请求的数据源对象的内容发生变更后,所有图片都会一闪而过;如果数据源的内容没变,就不会发生

当网络请求的数据源对象的内容发生变更后,所有图片都会一闪而过;同样是网络请求,如果数据源的内容没变,就不会发生。


HarmonyOS
2024-10-09 11:15:36
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

当数据源变化时,LazyForEach的刷新机制会导致整个ListItem被重建。由于Image组件是异步刷新,所以视觉上图片会发生闪烁。为了解决这种情况我们应该使用@ObjectLink和@Observed去单独刷新数据源改变的listItem。

参考文档:https://docs.openharmony.cn/pages/v4.0/zh-cn/application-dev/quick-start/arkts-rendering-control-lazyforeach.md/#%E5%B8%B8%E8%A7%81%E4%BD%BF%E7%94%A8%E9%97%AE%E9%A2%98参考demo:

class BasicDataSource implements IDataSource {  
  private listeners: DataChangeListener[] = [];  
  private originDataArray: StringData[] = [];  
  
  public totalCount(): number {  
    return 0;  
  }  
  
  public getData(index: number): StringData {  
    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);  
    })  
  }  
}  
  
class MyDataSource extends BasicDataSource {  
  private dataArray: StringData[] = [];  
  
  public totalCount(): number {  
    return this.dataArray.length;  
  }  
  
  public getData(index: number): StringData {  
    return this.dataArray[index];  
  }  
  
  public addData(index: number, data: StringData): void {  
    this.dataArray.splice(index, 0, data);  
    this.notifyDataAdd(index);  
  }  
  
  public pushData(data: StringData): void {  
    this.dataArray.push(data);  
    this.notifyDataAdd(this.dataArray.length - 1);  
  }  
  
  public reloadData(): void {  
    this.notifyDataReload();  
  }  
}  
  
class StringData {  
  message: string;  
  imgSrc: Resource;  
  constructor(message: string, imgSrc: Resource) {  
    this.message = message;  
    this.imgSrc = imgSrc;  
  }  
}  
  
@Entry  
@Component  
struct MyComponent {  
  private moved: number[] = [];  
  private data: MyDataSource = new MyDataSource();  
  
  aboutToAppear() {  
    for (let i = 0; i <= 20; i++) {  
      this.data.pushData(new StringData(`Hello ${i}`, $r('app.media.img')));  
    }  
  }  
  
  build() {  
    List({ space: 3 }) {  
      LazyForEach(this.data, (item: StringData, index: number) => {  
        ListItem() {  
          Column() {  
            Text(item.message).fontSize(50)  
              .onAppear(() => {  
                console.info("appear:" + item.message)  
              })  
            Image(item.imgSrc)  
              .width(500)  
              .height(200)  
          }.margin({ left: 10, right: 10 })  
        }  
        .onClick(() => {  
          item.message += '00';  
          this.data.reloadData();  
        })  
      }, (item: StringData, index: number) => JSON.stringify(item))  
    }.cachedCount(5)  
  }  
}
分享
微博
QQ
微信
回复
2024-10-09 16:09:58
相关问题
lazyforeach替换数据源解决方案
614浏览 • 1回复 待解决
网络请求数据处理
325浏览 • 1回复 待解决