HarmonyOS 使用Tabs其中的某一项点击需要跳转到某一页面而不是切换,这种如何实现?能不能阻止点击事件的默认执行逻辑

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

tabBar没有点击拦截功能实现,可以使用TabsController自定义页签以及并在其中添加事件进行逻辑判断,

可以参考以下demo:

export class ButtonInfoModel {
  index: number = 0;
  info: string = 'home';
  title: string = 'Home';
}
const buttonInfo: ButtonInfoModel[] =
  [{ index: 0, info: 'home', title: 'Home' }, { index: 1, info: 'map', title: 'Map' },
    { index: 2, info: 'charging', title: 'Charging' }]

@Component
export struct Home {
  @State message: string = 'Home';

  build() {
    Row() {
      Column() {
        Text(this.message).fontSize(50).fontWeight(FontWeight.Bold)
        Text('点击之后无法进入charging页,会跳转map页').fontSize(20).fontWeight(FontWeight.Bold).onClick(() => {
          buttonInfo[2].info = "map"
        })
      }.width('100%')
    }.height('100%')
  }
}

@Component
export struct Map {
  @State message: string = 'Map';

  build() {
    Row() {
      Column() {
        Text(this.message).fontSize(50).fontWeight(FontWeight.Bold)
      }.width('100%')
    }.height('100%')
  }
}

@Component
export struct Charging {
  @State message: string = 'Charging';
  build() {
    Row() {
      Column() {
        Text(this.message).fontSize(50).fontWeight(FontWeight.Bold)
      }.width('100%')
    }.height('100%')
  }
}

@Entry
@Component
struct home {
  @StorageProp('currentBreakpoint') currentBreakpoint: string = 'sm';
  @State currentPageIndex: number = 0;
  @State tabArray: Array<number> = [0, 1, 2]
  private controller: TabsController = new TabsController()

  build() {
    Column() {
      Tabs({ barPosition: BarPosition.End, controller: this.controller }) {
        TabContent() {
          Home()
        }

        TabContent() {
          Map()
        }

        TabContent() {
          Charging()
        }
      }.barHeight('0vp').height('94%').vertical(this.currentBreakpoint === 'lg').scrollable(false)
      this.bottomNavigation()
    }.justifyContent(FlexAlign.SpaceBetween).backgroundColor("#f1f3f5")
  }

  @Builder
  bottomNavigation() {
    Row() {
      ForEach(this.tabArray, (item: number) => {
        this.BottomNavigation(buttonInfo[item])
      })
    }.height("50vp").backgroundColor(Color.White).justifyContent(FlexAlign.SpaceAround).width("100%")
  }
  @Builder
  BottomNavigation(button: ButtonInfoModel) {
    Column({ space: '10vp' }) {
      Text(button.title)
        .fontColor(this.currentPageIndex === button.index ? "#0587D7" : "#A5A9AD")
        .fontWeight(500)
        .textAlign(TextAlign.Center)
        .fontSize('10fp')
    }.alignItems(HorizontalAlign.Center).margin(-20).onClick(() => {
      if (button.index === 2 && button.info === 'map') {
        this.controller.changeIndex(1)
        this.currentPageIndex = 1
      } else {
        this.controller.changeIndex(button.index)
        this.currentPageIndex = button.index
      }
    })
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-24 19:11:55


相关问题
HarmonyOS 怎么删除cookie里某一项
721浏览 • 1回复 待解决
HarmonyOS 点击tabs如何跳转到二级页面
1187浏览 • 1回复 待解决
判断某一年是不是闰年
1194浏览 • 1回复 待解决
HarmonyOS h5返回上一页面时会闪
1033浏览 • 1回复 待解决
HarmonyOS 路由怎么销毁其中栈内一页
421浏览 • 1回复 待解决
Scroll中点击某一个层图片移动到顶端
1388浏览 • 1回复 待解决
HarmonyOS 如何只取消某一监听
830浏览 • 1回复 待解决
cmake编译时候信息能不能
1084浏览 • 1回复 待解决