HarmonyOS 如何对tab页生命周期监控

tab页没有生命周期,如何对tab页生命周期监控

HarmonyOS
2024-12-24 15:44:35
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

1.参考TabContent中的onWillShow和onWillHide事件https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/arkui-ts/ts-container-tabcontent.md#onwillhide12

2.尝试使用下onVisibleAreaChange回调,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-component-visible-area-change-event-V5

onVisibleAreaChange的demo:

@Entry
@Component
struct TabBarStyleExample1 {
  @State message:string = "hello word"
  @State color:Color = Color.Black
  @Builder
  MyColum(){
    Childs({colors:this.color,message:this.message})
  }
  build() {
    Column({ space: 5 }) {
      Text("底部页签样式")
      Column() {
        Tabs({ barPosition: BarPosition.End }) {
          TabContent() {
            this.MyColum()
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Pink'))
          .onWillShow(() => {
            this.message = "Pink"
            this.color = Color.Pink
            console.info("Pink will show")
          })
          .onWillHide(() => {
            this.message = "Blue1"
            console.info("Pink will hide")
          })

          TabContent() {
            this.MyColum()
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Yellow'))
          .onWillShow(() => {
            this.message = "Yellow"
            this.color = Color.Yellow
            console.info("Yellow will show")
          })
          .onWillHide(() => {
            this.message = "Blue1"
            console.info("Yellow will hide")
          })

          TabContent() {
            this.MyColum()
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Blue'))
          .onWillShow(() => {
            this.message = "Blue"
            this.color = Color.Blue
            console.info("Blue will show")
          })
          .onWillHide(() => {
            this.message = "Blue1"
            console.info("Blue will hide")
          })

          TabContent() {
            this.MyColum()
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Green'))
          .onWillShow(() => {
            this.message = "Green"
            this.color = Color.Green
            console.info("Green will show")
          })
          .onWillHide(() => {
            this.message = "Blue1"
            console.info("Green will hide")
          })
        }
        .vertical(false)
        .scrollable(true)
        .barMode(BarMode.Fixed)
        .onChange((index: number) => {
          console.info(index.toString())
        })
        .width('100%')
        .backgroundColor(0xF1F3F5)
      }.width('100%').height(200)

    }
  }
}

@Reusable
@Component
struct Childs{
  @State colors:Color = Color.Green;
  @Prop@Watch('onChage') message:string = ''

  onChage(){
    console.log(':::onChage')
  }

  build() {
    Column(){
      Text(this.message)
    }.width('100%').height('100%').backgroundColor(this.colors)
    .onVisibleAreaChange([0.0, 1.0], (isVisible: boolean, currentRatio: number) => {
      console.info(':::Test Text isVisible: ' + isVisible + ', currentRatio:' + currentRatio)
    })

  }
}
分享
微博
QQ
微信
回复
2024-12-24 18:11:04
相关问题
HarmonyOS tab组件生命周期问题
245浏览 • 1回复 待解决
如何监听subwindow生命周期
567浏览 • 1回复 待解决
HarmonyOS Navigation 生命周期
107浏览 • 1回复 待解决
HarmonyOS Navigation生命周期
389浏览 • 1回复 待解决
HarmonyOS NavDestination生命周期问题
253浏览 • 1回复 待解决
HarmonyOS 生命周期不触发
272浏览 • 1回复 待解决
监听Ability生命周期
1536浏览 • 1回复 待解决
如何知晓navigation组件生命周期
526浏览 • 1回复 待解决
HarmonyOS 生命周期的区别
394浏览 • 1回复 待解决
HarmonyOS Navigation生命周期问题
253浏览 • 1回复 待解决
HarmonyOS 模块生命周期管理
555浏览 • 1回复 待解决
如何监听AbilitySlice的生命周期
5436浏览 • 1回复 待解决
如何监听全局Ability生命周期
553浏览 • 1回复 待解决
TabContent 内容的生命周期
754浏览 • 1回复 待解决
swiper切换监听生命周期
1313浏览 • 1回复 待解决
HarmonyOS 首页组件生命周期问题
129浏览 • 1回复 待解决
HarmonyOS 页面生命周期问题
260浏览 • 1回复 待解决
HarmonyOS 生命周期回调触发
306浏览 • 1回复 待解决