class二次刷新渲染数组

当我们更新class类中的数据时,会使数组对应的属性及相应的样式刷新。

HarmonyOS
2024-05-26 15:32:36
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

使用的核心API

@Observed装饰器和@ObjectLink装饰器:嵌套类对象属性变化

核心代码解释

核心代码如下:

@Entry 
@Component 
struct Index { 
@State section: Setion = new Setion() 
​ 
build() { 
  Row(){ 
    Row() { 
      Column() { 
        Text('reset ui '+this.section.data.count) 
          .margin({ top: 40 }) 
          .fontSize(25) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            this.section = new Setion(); 
            this.section.data = new Param(); 
            this.section.data.setCount(1); 
          }) 
​ 
        if (this.section.data) { 
          Header({ currentIndex: this.section.data.count, callback: (idx: number) => { 
            this.section = new Setion() 
            this.section.data.count = idx 
          } }) 
            .margin({ top: 120 }) 
        } 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
} 
​ 
@Component 
struct Header { 
@Prop currentIndex: number 
callback?: (index: number) => void 
​ 
build() { 
  List() { 
    ForEach(['1', '2', '3'], (content: string, index: number) => { 
      this.tabItem(content, index, () => { 
        this.currentIndex = index 
        if (this.callback) { 
          this.callback(index) 
        } 
      }) 
    }) 
  } 
  .height(28) 
  .listDirection(Axis.Horizontal) 
  .scrollBar(BarState.Off) 
  .edgeEffect(EdgeEffect.Spring, { alwaysEnabled: false }) 
} 
​ 
@Builder 
tabItem(item: string, index: number, click: () => void) { 
  Text(item) 
    .fontSize(12) 
    .fontColor(index == this.currentIndex ? '0x3377ff': 
      '0x222222') 
    .fontWeight(FontWeight.Medium) 
    .padding({ left: 12, right: 12, top: 7, bottom: 7 }) 
    .borderRadius(2) 
    .height(28) 
    .margin({ left: 8, bottom: 8 }) 
    .borderWidth(index == this.currentIndex ? 1 : 0) 
    .borderColor('0x4d3377ff') 
    .onClick(click) 
} 
} 
​ 
@Observed 
export class Setion { 
data: Param = new Param() 
​ 
​ 
} 
​ 
@Observed 
export class Param { 
count: number = 0 
​ 
setCount(count: number){ 
  this.count =count; 
} 
}
  • 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.

实现效果

分享
微博
QQ
微信
回复
2024-05-27 20:33:33
相关问题
HarmonyOS aioxs二次封装
578浏览 • 1回复 待解决
HarmonyOS 权限二次申请
406浏览 • 1回复 待解决
HarmonyOS getStringByName方法二次封装
595浏览 • 1回复 待解决
实现二次侧滑退出应用
2358浏览 • 1回复 待解决
HarmonyOS cocos引擎能否二次启动
905浏览 • 1回复 待解决
华为账号实时验证/二次放号相关咨询
2356浏览 • 1回复 待解决
HarmonyOS 数组刷新问题
736浏览 • 1回复 待解决