HarmonyOS lazyforEach渲染问题

class BasicDataSource implements IDataSource {
  private listeners: DataChangeListener[] = [];
  private originDataArray: NestedString[] = [];

  public totalCount(): number {
    return 0;
  }

  public getData(index: number): NestedString {
    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: NestedString[] = [];

  public totalCount(): number {
    return this.dataArray.length;
  }

  public getData(index: number): NestedString {
    return this.dataArray[index];
  }

  public addData(index: number, data: NestedString): void {
    this.dataArray.splice(index, 0, data);
    this.notifyDataAdd(index);
  }

  public pushData(data: NestedString): void {
    this.dataArray.push(data);
    this.notifyDataAdd(this.dataArray.length - 1);
  }
}


@Observed
class NestedString {
  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 NestedString(`Hello ${i}`));
    }
  }

  build() {
    List({ space: 3 }) {
      LazyForEach(this.data, (item: NestedString, index: number) => {
        ListItem() {
          Row() {
            // 这里的不改变  内侧的改变
            Text(item.message).fontSize(50)
            ChildComponent({ data: item })
          }
        }
        .onClick(() => {
          item.message += '0'
        })
      }, (item: NestedString, index: number) => JSON.stringify(item) + index.toString())
    }.cachedCount(5)
  }
}

@Component
struct ChildComponent {
  @ObjectLink data: NestedString

  build() {
    Row() {
      Text(this.data.message).fontSize(50)
        .onAppear(() => {
          console.info("appear:" + this.data.message)
        })
    }.margin({ left: 10, right: 10 })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.

同层级的ui没有修改,子元素的反而更改了。

HarmonyOS
2024-12-25 12:09:29
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

对于Text组件来说item.message属性改变是多层嵌套的变化无法观察到所以未引起当前UI的刷新:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-observed-and-objectlink-V5#基础嵌套对象属性更改失效

分享
微博
QQ
微信
回复
2024-12-25 15:29:12
相关问题
HarmonyOS LazyForEach问题刷新UI问题
939浏览 • 1回复 待解决
HarmonyOS swiper + LazyForEach使用问题
1140浏览 • 1回复 待解决
HarmonyOS lazyForEach数据应用问题
682浏览 • 1回复 待解决
HarmonyOS lazyForeach嵌套视图问题
887浏览 • 1回复 待解决
HarmonyOS lazyForEach的key问题
450浏览 • 1回复 待解决
HarmonyOS LazyForEach组件dataSource使用问题
1531浏览 • 2回复 待解决
HarmonyOS LazyForEach数据刷新问题
659浏览 • 1回复 待解决
HarmonyOS 界面数据渲染问题
897浏览 • 1回复 待解决
HarmonyOS 富文本渲染问题
1157浏览 • 1回复 待解决
HarmonyOS LazyForEach
829浏览 • 1回复 待解决
使用LazyForEach懒加载列表相关问题
1726浏览 • 1回复 待解决
HarmonyOS ArkWeb同层渲染宽高问题
645浏览 • 1回复 待解决