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; 
} 
}

实现效果

分享
微博
QQ
微信
回复
2024-05-27 20:33:33
相关问题
实现二次侧滑退出应用
415浏览 • 1回复 待解决
动画如何做渲染不消失
6179浏览 • 1回复 待解决
readonly修饰的数组无法获取数组元素
479浏览 • 1回复 待解决
数组嵌套数组场景的懒加载实现
208浏览 • 1回复 待解决
NAPI中定义并注册Class
281浏览 • 1回复 待解决
napi常见用法:class对象绑定
400浏览 • 1回复 待解决
ForEach数组数据无法传输
3886浏览 • 1回复 待解决