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

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

HarmonyOS
2024-10-15 09:38:35
浏览
收藏 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%')  
  }  
}
分享
微博
QQ
微信
回复
2024-10-15 16:34:37
相关问题
怎么获取List里面每个itemposition?
297浏览 • 1回复 待解决
HarmonyOS @State无法更新组件文本
22浏览 • 1回复 待解决
HarmonyOS " @State可以修饰ArrayList吗"
407浏览 • 1回复 待解决
HarmonyOS @State可以修饰对象数组
32浏览 • 1回复 待解决
HarmonyOS @state可以修饰对象数组么
41浏览 • 1回复 待解决
HarmonyOS @State 是不是不能修饰枚举
107浏览 • 1回复 待解决