用@State修饰的Array内部某个item更新了字段,怎么同步通知到ListItem组件里面?

用@State修饰的Array内部某个item更新了字段,怎么同步通知到ListItem组件里面?

HarmonyOS
2024-10-15 09:38:35
556浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

参考下这个demo:

struct Index {  
  private tabController: TabsController = new TabsController()  
  @State currentIndex: number = 0  
  @State provinceData: AreaItemEntity = new AreaItemEntity('', '', false);  
  @State cityData?: AreaItemEntity | null = null  
  @State proList: Array<AreaItemEntity> = []  
  @State cityList: Array<AreaItemEntity> = []  
  @State selectProId: string = ''  
  getCityArray(id: string): Array<AreaItemEntity> {  
    if (id === '1') {  
      let newarray: Array<AreaItemEntity> = [new AreaItemEntity('南京市', '1', false)]  
      return newarray  
    } else {  
      let newarray: Array<AreaItemEntity> = [new AreaItemEntity('合肥市', '1', false)]  
      return newarray  
    }  
  }  
  build() {  
    Column() {  
  
      Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.tabController }) {  
        TabContent() {  
          ProvinceTab({  
            onItemSelected: (selectData: AreaItemEntity) => {  
              this.provinceData = selectData  
            },  
            selectProId: this.selectProId  
          }  
          )  
        }.tabBar(this.provinceData == null ? "请选择" : this.provinceData.name)  
        TabContent() {  
          CityTab({  
            onItemSelected: (selectData: AreaItemEntity) => {  
              this.cityData = selectData  
            },  
            dataList: this.cityList,  
          }  
          )  
        }.tabBar(this.cityData == null ? "请选择" : this.cityData.name)  
      }  
      .vertical(false)  
      .barMode(BarMode.Scrollable, { margin: 10, nonScrollableLayoutStyle: LayoutStyle.SPACE_BETWEEN_OR_CENTER })  
      .barHeight(50)  
      .barWidth('100%')  
      .layoutWeight(1)  
      .animationDuration(400)  
      .onChange((index: number) => {  
        this.currentIndex = index  
        if (index === 1) {  
          this.cityList = this.getCityArray(this.selectProId)  
        }  
      })  
      .width('100%')  
      .height('100%')  
      .backgroundColor('#FFFFFF')  
    }.height('75%').width('100%').border({ radius: 10 }).padding(20)  
  }  
}  
  
@Component  
export struct ProvinceTab {  
  onItemSelected: (selectData: AreaItemEntity) => void = () => {  
  }  
  @State dataList: Array<AreaItemEntity> = []  
  @Link selectProId: string  
  build() {  
    Column() {  
      List() {  
        ForEach(this.dataList, (item: AreaItemEntity) => {  
          ListItem() {  
            Column() {  
              Text(item.name).fontColor(item.isSelected ? "#1E6EFA" : "#000000")  
            }.alignItems(HorizontalAlign.Center)  
            .justifyContent(FlexAlign.Center)  
            .width('100%')  
            .onClick(() => {  
              let dataIndex = this.dataList?.indexOf(item)  
              this.setDataSelected(dataIndex)  
              this.onItemSelected(item)  
              this.selectProId = item.id  
            })  
  
          }.width('100%').padding("10vp")  
  
        }, (item: AreaItemEntity) => item.name)  
      }.width('100%')  
      .height('100')  
      .layoutWeight(1)  
    }.width('100%').height('100%')  
  }  
}  
@Component  
export struct CityTab {  
  onItemSelected: (selectData: AreaItemEntity) => void = () => {  
  }  
  @Link dataList: Array<AreaItemEntity>  
  build() {  
    Column() {  
      List() {  
        ForEach(this.dataList, (item: AreaItemEntity) => {  
          ListItem() {  
            Column() {  
              Text(item.name).fontColor(item.isSelected ? "#1E6EFA" : "#000000")  
            }.alignItems(HorizontalAlign.Center)  
            .justifyContent(FlexAlign.Center)  
            .width('100%')  
            .onClick(() => {  
              let dataIndex = this.dataList?.indexOf(item)  
              this.setDataSelected(dataIndex)  
              this.onItemSelected(item)  
  
            })  
          }.width('100%').padding("10vp")  
        }, (item: AreaItemEntity) => item.name)  
      }.width('100%')  
      .height('100')  
      .layoutWeight(1)  
    }.width('100%').height('100%')  
  }  
}
  • 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.
分享
微博
QQ
微信
回复
2024-10-15 16:34:37
相关问题
怎么获取List里面每个itemposition?
701浏览 • 1回复 待解决
HarmonyOS @State无法更新组件文本
573浏览 • 1回复 待解决
HarmonyOS @State可以修饰对象数组
690浏览 • 1回复 待解决
HarmonyOS " @State可以修饰ArrayList吗"
1130浏览 • 1回复 待解决
HarmonyOS @State 是不是不能修饰枚举
967浏览 • 1回复 待解决