HarmonyOS 使用tab组件,列表对象如何动态更新内容。

使用tab组件,通过列表组件创建自定义tabBar,如何动态的更新列表tabbar的部分字段,普通对象可以动态更新,列表元素数据更新失败。

HarmonyOS
2024-12-24 16:44:36
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

可以使用@ObservedV2和@Trace使得被装饰的类和属性具有深度观测的能力。

参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-new-observedv2-and-trace-V5

builder函数传引用说明:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-two-way-sync-V5

示例demo如下:

import { promptAction } from '@kit.ArkUI';

@Entry
@ComponentV2
struct Index {
  temp:Tmp = new Tmp()
  aboutToAppear(): void {
    let cateItem: CataLogItems = new CataLogItems();
    cateItem.catalog = '全场换购'
    cateItem.catalogChangeNum = '0'
    cateItem.catalogLimitNum = 5
    cateItem.catalogIndex = '0'

    let cateItemTwo: CataLogItems = new CataLogItems();
    cateItemTwo.catalog = '全场6折'
    cateItemTwo.catalogChangeNum = '1'
    cateItemTwo.catalogLimitNum = 5
    cateItemTwo.catalogIndex = '1'

    this.temp.cateItem.catalog = '110'
    this.temp.cateList.push(cateItem)
    this.temp.cateList.push(cateItemTwo)
  }

  build() {
    Column() {
      // 真实场景
      Tabs({ barPosition: BarPosition.Start}){
        ForEach( this.temp.cateList, (item: CataLogItems, index: number) => {
          TabContent(){
            Column(){
              Text('商品列表').fontSize(20).margin({bottom: 20})
              Text(item.catalog).fontSize(20)
            }.width('100%').height('100%').onClick(()=>{
              item.catalog = 'xxxx'
              promptAction.showToast({message: '修改tab标题为:xxxx'});
            })
          }.backgroundColor('#3300ff00').onClick(()=>{
            this.temp.curIndex = index;
          })
          .tabBar(this.tabBuilder(item, '50%', index)).width('50%')
        })
      }.layoutWeight(1).onChange((index)=>{
        this.temp.curIndex = index;
      }).height(200)

      // 测试demo
      Column() {
        overBuilder(this.temp)
        Button('更新组件状态').onClick(() => {
          this.temp.subTitle = 'Hello World'
          this.temp.cateItem.catalog = '119'
          const data = this.temp.cateList[0]
          data.catalog = '119'
          this.temp.cateList[0] = data
        })
      }
    }
    .height('100%').width('100%')
    .padding({left: 16, right: 16})
    .justifyContent(FlexAlign.Center)
  }

  @Builder tabBuilder(item: CataLogItems, itemWidth: string|number, index: number) {
    Column() {
      Row() {
        Text(item.catalog).fontSize(15).height(20).margin({right: 3})
          .maxLines(1).textOverflow({ overflow: TextOverflow.Ellipsis })
          .fontWeight(this.temp.curIndex == index ? FontWeight.Bold : FontWeight.Normal)
          .fontColor(this.temp.curIndex == index ? '#09AFFF' : '#333333')
        Text(`(已选${item.catalogChangeNum}/${item.catalogLimitNum})`).fontSize(12).fontColor('#666666')
      }
      .justifyContent(FlexAlign.Center).height(20)

      Stack().width(30).height(3).borderRadius(1.5).backgroundColor('#09AFFF')
    }
    .width(itemWidth).height(30).alignItems(HorizontalAlign.Center)
    .padding({ left: 9, right: 9 })
  }
}

@ObservedV2
class Tmp {
  @Trace subTitle: string = 'Hello';
  @Trace cateItem: CataLogItems = new CataLogItems()
  @Trace cateList: CataLogItems[] = []
  @Trace curIndex: number = 0;
  @Trace currentCatalogIndex: string|undefined = '0';
}

@ObservedV2
export class CataLogItems {
  @Trace catalog?:string
  @Trace catalogChangeNum?:string
  @Trace catalogIndex?:string
  @Trace catalogLimitNum:number = 5
  @Trace pageNum:number = 1;
  @Trace pageSize:number = 20;
  @Trace totalNumber:number = 0;
  @Trace totalPage?:string
}

@Builder function overBuilder($$: Tmp) {
  Column() {
    Text(`(字符串: ${$$.subTitle})`).fontSize(16).fontColor('#666666').margin({bottom: 12})
    Text(`(对象: ${$$.cateItem.catalog})`).fontSize(16).fontColor('#666666').margin({bottom: 12})
    Text(`(数组元素: ${$$.cateList[0].catalog})`).fontSize(16).fontColor('#666666').margin({bottom: 12})
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-24 19:28:40
相关问题
Canvas绘制内容如何动态更新
2809浏览 • 1回复 待解决
HarmonyOS Canvas绘制内容如何更新
645浏览 • 1回复 待解决
HarmonyOS list嵌套tab列表高度变化
687浏览 • 1回复 待解决
按钮内子控件如何动态更新
1365浏览 • 1回复 待解决
如何更新页面列表数据
7975浏览 • 1回复 待解决
HarmonyOS 如何获取对象的方法列表
1143浏览 • 1回复 待解决