HarmonyOS 如何监听数组对象中元素属性的变化

HarmonyOS
2024-12-25 08:43:22
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

如何监听数组内对象属性变化 具体实现方法参考地址:https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs-V5/faqs-arkui-213-V5

参考示例:

@Observed
export class SoundBean {
  fName: string = '';
  isPlaying: boolean = false;
  testClass: TestClass = new TestClass();
  array: TestClass[] = [new TestClass(),new TestClass()]
}
@Observed
export class TestClass {
  name: string = "123";
  six: string = "456";
}
@Entry
@Component
struct Index1 {
  scroller:Scroller = new Scroller()
  @State sounds:Array<SoundBean> = new Array()
  aboutToAppear(): void {
    for (let i = 0; i < 5; i++) {
      let obj:SoundBean = new SoundBean()
      obj.fName= `${i}`
      this.sounds.push(obj)
    }
  }
  @Builder
  getListView() {
    List({ scroller: this.scroller }) {
      ForEach(this.sounds, (soundBean: SoundBean, index: number) => {
        ListItem() {
          AlbumItemView({ soundBean: soundBean })
        }.onClick(() => {
          for (let i = 0; i < this.sounds.length; i++) {
            if (i === index) {
              this.sounds[i].isPlaying = true
              this.sounds[i].fName = 'aaaaa'
              this.sounds[i].testClass.name = "哈哈哈哈"
              this.sounds[i].array[0].six = "男"
            } else {
              this.sounds[i].isPlaying = false
            }
          }
        })
      })
    }
    .width('100%')
    .height('100%')
    .nestedScroll({
      scrollForward: NestedScrollMode.PARENT_FIRST,
      scrollBackward: NestedScrollMode.SELF_FIRST
    })
    .divider({
      strokeWidth: 0.5,
      startMargin: 50,
      endMargin: 0,
      color: '#e5e5e5'
    })
    .edgeEffect(EdgeEffect.None)
    .backgroundColor(Color.White)
  }
  build() {
    Column(){
      this.getListView()
    }
  }
}
@Component
export struct AlbumItemView {
  @ObjectLink soundBean: SoundBean
  build() {
    Row() {
      Row() {
        Column() {
          Text(this.soundBean.testClass.name)
            .fontSize(14).fontColor(this.soundBean.isPlaying ? Color.Green : "#282828")
            .margin({ top: 4 })
        }
        .justifyContent(FlexAlign.Center)
        .alignItems(HorizontalAlign.Start)
        .width(310)
        .height("100%")
        Text(this.soundBean.array[0].six)
          .fontSize(14).fontColor(this.soundBean.isPlaying ? Color.Green : "#282828")
      }.width(330)
      .justifyContent(FlexAlign.SpaceBetween)
    }
    .width("100%")
    .padding({ left: 15, right: 15 })
    .height(58)
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-25 10:00:59


相关问题
如何监听数组对象属性变化
3140浏览 • 1回复 待解决
数组中元素变更如何触发刷新list?
863浏览 • 1回复 待解决
readonly修饰数组无法获取数组元素
3089浏览 • 1回复 待解决