HarmonyOS 提供一个关于自定义对象类刷新的的Demo

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

请参考以下demo:

class BasicDataSource implements IDataSource {
  private listeners: DataChangeListener[] = [];
  private originDataArray: StringData[] = [];
  public totalCount(): number {
    return 0;
  }
  public getData(index: number): StringData | undefined {
    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);
  }
  public pushArrayData(newData: StringData): void {
    this.clear();
    this.dataArray.push(newData);
    this.notifyDataReload();
  }
  public clear(): void {
    this.dataArray.splice(0, this.dataArray?.length)
  }
}
@Observed
class StringData {
  message: string;
  constructor(message: string) {
    this.message = message;
  }
}
@Entry
@Component
struct Index {
  private moved: number[] = [];
  @State data: MyDataSource = new MyDataSource();

  aboutToAppear() {
    for (let i = 0; i <= 50; i++) {
      this.data.pushData(new StringData(`Hello ${i}`));
    }
  }
  build() {
    Column(){
      Button("点击刷新数据源(更新Hello 5)")
        .onClick(() => {
          this.data.clear();
          for (let i = 0; i <= 50; i++) {
            if (i == 5) {
              this.data.pushData(new StringData(`Hello 500`));
            } else {
              this.data.pushData(new StringData(`Hello ${i}`));
            }
          }
        })
      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() {
      Text(this.data.message).fontSize(50)
        .onAppear(() => {
          console.info("appear:" + this.data.message)
        })
    }.margin({ left: 10, right: 10 })
  }
}
分享
微博
QQ
微信
回复
15h前
相关问题
提供一个关于地图组件使用demo
596浏览 • 1回复 待解决
能否提供一个关于SM3加密demo
713浏览 • 1回复 待解决
HarmonyOS一个自定义tabs冲突
43浏览 • 1回复 待解决
HarmonyOS提供自定义组件封装demo
448浏览 • 2回复 待解决
HarmonyOS 实现一个自定义分类列表
321浏览 • 1回复 待解决
如何实现一个自定义样式toast提示
2005浏览 • 1回复 待解决
json 如何转换成自定义业务对象
818浏览 • 1回复 待解决
如何定义一个未知类型对象
911浏览 • 2回复 待解决