Tabs禁止系统切换,在使用了自定义导航栏后,切换页签的逻辑需要手动实现。

在使用了自定义导航栏后,切换页签的逻辑需要手动实现。即用户点击对应页签时,屏幕需要显示相应的内容页。但是使用了自定义导航栏后系统还是响应切换逻辑,希望能够禁用系统切换逻辑实现自定义。

HarmonyOS
2024-05-26 11:02:41
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Nicrosoft

使用的核心API

tab

核心代码解释

不要直接 .tabBar(this.tab(item, 0, $r('app.media.startIcon'), $r('app.media.startIcon')))

tabbar自定义实现即可解决禁用系统切换逻辑

@Entry 
@Component 
struct Drag2 { 
  @State tabArray: Array<number> = [0, 1,2] 
  @State focusIndex: number = 0 
  @State pre: number = 0 
  @State index: number = 0 
  private controller: TabsController = new TabsController() 
  @State test: boolean = false 
  // 单独的页签 
  @Builder 
  Tab(tabName: string, tabItem: number, tabIndex: number) { 
    Row({ space: 20 }) { 
      Text(tabName).fontSize(18) 
      Image($r('app.media.icon')).width(20).height(20) 
    } 
    .justifyContent(FlexAlign.Center) 
    .constraintSize({ minWidth: 35 }) 
    .width(100) 
    .height(30) 
    .borderRadius({ topLeft: 10, topRight: 10 }) 
    .onClick(() => { 
      this.controller.changeIndex(tabIndex) 
      this.focusIndex = tabIndex 
    }) 
    .backgroundColor(tabIndex === this.focusIndex ? "#ffffffff" : "#ffb7b7b7") 
  } 
  build() { 
    Column() { 
      Column() { 
        // 页签 
        Row({ space: 7 }) { 
          Scroll() { 
            Row() { 
              ForEach(this.tabArray, (item: number, index: number) => { 
                this.Tab("页签 " + item, item, index) 
              }) 
            } 
            .justifyContent(FlexAlign.Start) 
          } 
          .align(Alignment.Start) 
          .scrollable(ScrollDirection.Horizontal) 
          .scrollBar(BarState.Off) 
          .width('90%') 
          .backgroundColor("#ffb7b7b7") 
        } 
        .alignItems(VerticalAlign.Bottom) 
        .width('100%') 
        .backgroundColor("#ffb7b7b7") 
      } 
      .alignItems(HorizontalAlign.Start) 
      .width('100%') 
      //tabs 
      Tabs({ barPosition: BarPosition.Start, controller: this.controller }) { 
        ForEach(this.tabArray, (item: number, index: number) => { 
          TabContent() { 
            Text('我是页面 ' + item + " 的内容") 
              .height(300) 
              .width('100%') 
              .fontSize(30) 
          } 
          .backgroundColor(Color.Blue) 
        }) 
      } 
      .width('100%') 
      .barHeight(0) 
      .animationDuration(100) 
      .onChange((index: number) => { 
        console.log('foo change') 
        this.focusIndex = index}) 
    } 
    .height('100%') 
  } 
}

实现效果

适配版本信息

SDK:4.1.0.52

IDE:DevEco Studio 4.1.1.400

分享
微博
QQ
微信
回复
2024-05-27 10:45:02
相关问题
如何禁止Tabs系统切换逻辑
570浏览 • 0回复 待解决
Tabs组件自定义导航UI问题
451浏览 • 1回复 待解决
Navigation实现Tabs切换效果
476浏览 • 1回复 待解决
如何自定义模拟Tabs组件
373浏览 • 1回复 待解决
鸿蒙tablist 如何通过滑动切换页面?
5180浏览 • 1回复 待解决
getContext(this)能否自定义类中使用
904浏览 • 1回复 待解决
Tabs选项绑定onClick事件之后无法切换
588浏览 • 0回复 待解决
如何自定义Video组件控制样式
807浏览 • 1回复 待解决