HarmonyOS List中的图片闪烁

LazyForEach渲染List,当更新数据源的时候列表中的图片会闪一下

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

若仅靠LazyForEach的刷新机制,当item变化时若想更新子组件,需要将原来的子组件全部销毁再重新构建,在子组件结构较为复杂的情况下,靠改变键值去刷新渲染性能较低。因此框架提供了@Observed与@ObjectLink机制进行深度观测,可以做到仅刷新使用了该属性的组件,提高渲染性能,避免更新listitem时图片闪烁问题

详细请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-rendering-control-lazyforeach-V5

改变数据子属性@Observed与@ObjectLink的使用

详细参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-observed-and-objectlink-V5#在子组件中给objectlink装饰的变量赋值

可参考如下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);
    })
  }
  notifyDataMove(from: number, to: number): void {
    this.listeners.forEach(listener => {
      listener.onDataMove(from, to);
    })
  }
}
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);
  }
}
@Observed
class StringData {
  message: string;
  constructor(message: string) {
    this.message = message;
  }
}
@Entry
@Component
struct MyComponent {
  private moved: number[] = [];
  @State data: MyDataSource = new MyDataSource();
  aboutToAppear() {
    for (let i = 0; i <= 20; i++) {
      this.data.pushData(new StringData(Hello ${i}));
    }
  }
  build() {
    List({ space: 3 }) {
      LazyForEach(this.data, (item: StringData, index: number) => {
        ListItem() {
          ChildComponent({data: item})
        }
        .onClick(() => {
          item.message += '0';
        })
      }, (item: StringData, index: number) => index.toString())
    }.cachedCount(5)
  }
}
@Component
struct ChildComponent {
  @ObjectLink data: StringData
  build() {
    Row() {
      Image($r("app.media.startIcon"))
        .width(100)
        .height(100)
      Text(this.data.message).fontSize(50)
        .onAppear(() => {
          console.info("appear:" + this.data.message)
        })
    }.margin({ left: 10, right: 10 })
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS list数据刷新,头像闪烁
32浏览 • 1回复 待解决
Swiper 组件嵌套图片刷新数据会闪烁
1278浏览 • 1回复 待解决
HarmonyOS List嵌套list布局
41浏览 • 1回复 待解决
HarmonyOS 加载启动图轻微闪烁
32浏览 • 1回复 待解决
解决Canvas画布缩放时闪烁
1529浏览 • 1回复 待解决
HarmonyOS listitem交互效果处理
429浏览 • 1回复 待解决
HarmonyOSList是如何加载item
244浏览 • 2回复 待解决
HarmonyOS List字体大小全局修改
27浏览 • 1回复 待解决
HarmonyOS list属性chainAnimation作用
24浏览 • 1回复 待解决
redis List 操作是什么?
3069浏览 • 1回复 待解决
HarmonyOSList组件是否支持局部刷新
638浏览 • 1回复 待解决