HarmonyOS 如何动态添加TabContent以及自定义tabbarUI刷新问题?

HarmonyOS 如何动态添加TabContent以及自定义tabbarUI刷新问题?

HarmonyOS
2024-10-08 11:29:55
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

请参考demo:

index.ets

import { UserInfo } from './UserInfo'  
import { router } from '@kit.ArkUI';  
  
//写在ability更合适  
AppStorage.setOrCreate('userInfo', new UserInfo("name1"));  
  
@Entry  
@Component  
struct TabsExample {  
  @State fontColor: string = '#182431'  
  @State selectedFontColor: string = '#007DFF'  
  @State currentIndex: number = 0  
  private controller: TabsController = new TabsController()  
  @State userInfo: SubscribedAbstractProperty<UserInfo> = AppStorage.link('userInfo');  
  onPageShow(): void {  
    this.userInfo = AppStorage.link('userInfo')  
  }  
  @Builder  
  tabBuilder(index: number, name: string) {  
    Column() {  
      Text(name)  
        .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)  
        .fontSize(16)  
        .fontWeight(this.currentIndex === index ? 500 : 400)  
        .lineHeight(22)  
        .margin({ top: 17, bottom: 7 })  
      Divider()  
        .strokeWidth(2)  
        .color('#007DFF')  
        .opacity(this.currentIndex === index ? 1 : 0)  
    }.width('100%')  
  }  
  build() {  
    Column() {  
      Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) {  
        TabContent() {  
          Column().width('100%').height('100%').backgroundColor('#00CB87')  
        }.tabBar(this.tabBuilder(0, 'green'))  
        TabContent() {  
          Column().width('100%').height('100%').backgroundColor('#007DFF')  
        }.tabBar(this.tabBuilder(1, 'blue'))  
        TabContent() {  
          Column().width('100%').height('100%').backgroundColor('#FFBF00')  
        }.tabBar(this.tabBuilder(2, 'yellow'))  
        TabContent() {  
          Info({ userInfo: this.userInfo.get() })  
            .onClick(() => {  
              router.pushUrl({  
                url: 'pages/login'  
              })  
            })  
        }.tabBar(this.tabBuilder(3, 'pink'))  
      }  
      .vertical(false)  
      .barMode(BarMode.Fixed)  
      .barWidth(360)  
      .barHeight(56)  
      .animationDuration(400)  
      .onChange((index: number) => {  
        this.currentIndex = index  
      })  
      .width(360)  
      .height(296)  
      .margin({ top: 52 })  
      .backgroundColor('#F1F3F5')  
    }.width('100%')  
  }  
}  
@Component  
struct Info {  
  @Prop userInfo: UserInfo  
  build() {  
    Text(this.userInfo.name)  
  }  
}
  • 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.

login.ets

import { UserInfo } from './UserInfo';  
AppStorage.setOrCreate('name', "nameundefined");  
@Entry  
@Component  
struct Login {  
  @State  
  userInfo: SubscribedAbstractProperty<UserInfo> = AppStorage.link('userInfo');  
  build() {  
    Column() {  
      Text(this.userInfo.get().name)  
      Button("点击按钮登录").onClick(() => {  
        this.userInfo.set(new UserInfo("nameY"))  
      })  
    }.width('100%')  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

UserInfo.ets

@Observed  
export class UserInfo {  
  name: string  
  
  constructor(name: string) {  
    this.name = name  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

上述demo是模拟了登录页登录后更新个人信息,切换tab后更新个人信息。

分享
微博
QQ
微信
回复
2024-10-08 17:02:35


相关问题
HarmonyOS 自定义弹窗刷新问题
691浏览 • 1回复 待解决
HarmonyOS 自定义刷新空间问题
595浏览 • 1回复 待解决
HarmonyOS 页面刷新问题
1102浏览 • 1回复 待解决
HarmonyOS webview刷新问题
814浏览 • 2回复 待解决
HarmonyOS 列表刷新问题
1323浏览 • 1回复 待解决
HarmonyOS 组件刷新问题
772浏览 • 1回复 待解决
HarmonyOS Tabs组件怎么动态添加TabContent
1020浏览 • 1回复 待解决
HarmonyOS 数据刷新问题
767浏览 • 1回复 待解决
HarmonyOS List item 刷新问题
1525浏览 • 1回复 待解决
HarmonyOS ForEach列表刷新问题
922浏览 • 1回复 待解决
HarmonyOS 页面数据刷新问题
895浏览 • 1回复 待解决
HarmonyOS LazyForEach数据刷新问题
679浏览 • 1回复 待解决
NativeWindowFlushBuffer接口刷新问题
2546浏览 • 1回复 待解决
HarmonyOS @Builder UI刷新问题
741浏览 • 1回复 待解决
HarmonyOS UI 未刷新问题
1366浏览 • 0回复 待解决
HarmonyOS swiper数据刷新问题
651浏览 • 1回复 待解决
HarmonyOS UI不刷新问题
814浏览 • 1回复 待解决
HarmonyOS 页面跳转刷新问题
2003浏览 • 1回复 待解决
HarmonyOS Refresh自定义刷新样式
822浏览 • 1回复 待解决
自定义组件中如何添加图片?
3399浏览 • 1回复 待解决