Tabs组件嵌套滑动组件

Tabs组件嵌套滑动组件(Tabs/List等)事件处理

一级Tabs组件下嵌套个二级的Tabs,二级Tabs第一个页面左滑或最后一个页面右滑不能切换一级Tabs标签,可以通过系统api或者自定义事件拦截处理吗?

HarmonyOS
2024-05-23 23:44:45
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
xbibi

使用的核心API

  • tab
  • gesture

核心代码解释

tab嵌套,在二级tab组件里面最边缘页签添加手势,通过changeIndex切换到一级tab页面。

// xxx.ets 
@Entry 
@Component 
struct TabsExample { 
  @State fontColor: string = '#182431' 
  @State selectedFontColor: string = '#007DFF' 
  @State currentIndex: number = 0 
  private controller: TabsController = new TabsController() 
  
  @State subCurrentIndex: number = 0 
  private subController: TabsController = new TabsController() 
  
  private panOptionRight: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Left }) 
  private panOptionLeft: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Right }) 
  
  
  @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%') 
  } 
  
  @Builder subTabBuilder(index: number, name: string) { 
    Column() { 
      Text(name) 
        .fontColor(this.subCurrentIndex === index ? this.selectedFontColor : this.fontColor) 
        .fontSize(16) 
        .fontWeight(this.subCurrentIndex === index ? 500 : 400) 
        .lineHeight(22) 
        .margin({ top: 17, bottom: 7 }) 
      Divider() 
        .strokeWidth(2) 
        .color('#007DFF') 
        .opacity(this.subCurrentIndex === index ? 1 : 0) 
    }.width('100%') 
  } 
  
  build() { 
    Column() { 
      Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) { 
        TabContent() { 
          Column().width('100%').height('100%').backgroundColor('#ffb554d7') 
        }.tabBar(this.tabBuilder(0, '首页')) 
  
        TabContent() { 
          Column(){ 
            Tabs({ barPosition: BarPosition.Start,controller: this.subController }) { 
              TabContent() { 
                Column().width('100%').height('100%').backgroundColor('#00CB87') 
                  // 左右拖动触发该手势事件 
                  .gesture( 
                    PanGesture(this.panOptionLeft) 
                      .onActionStart((event?: GestureEvent) => { 
                        console.info('Pan start') 
                      }) 
                      .onActionUpdate((event?: GestureEvent) => { 
                        if (event) { 
  
                        } 
                      }) 
                      .onActionEnd(() => { 
                        this.controller.changeIndex(0) 
                        console.info('Pan end') 
                      }) 
                  ) 
              }.tabBar(this.subTabBuilder(0, 'green')) 
  
              TabContent() { 
                Column().width('100%').height('100%').backgroundColor('#007DFF') 
              }.tabBar(this.subTabBuilder(1, 'blue')) 
  
              TabContent() { 
                Column().width('100%').height('100%').backgroundColor('#FFBF00') 
              }.tabBar(this.subTabBuilder(2, 'yellow')) 
  
              TabContent() { 
                Column().width('100%').height('100%').backgroundColor('#E67C92') 
                  // 左右拖动触发该手势事件 
                  .gesture( 
                    PanGesture(this.panOptionRight) 
                      .onActionStart((event?: GestureEvent) => { 
                        console.info('Pan start') 
                      }) 
                      .onActionUpdate((event?: GestureEvent) => { 
                        if (event) { 
  
                        } 
                      }) 
                      .onActionEnd(() => { 
                        this.controller.changeIndex(2) 
                        console.info('Pan end') 
                      }) 
                  ) 
              }.tabBar(this.subTabBuilder(3, 'pink')) 
            } 
            .vertical(false) 
            .barMode(BarMode.Fixed) 
            .barWidth(360) 
            .barHeight(56) 
            .animationDuration(400) 
            .onChange((index: number) => { 
              this.subCurrentIndex = index 
            }) 
            .width(360) 
            .backgroundColor('#F1F3F5') 
          } 
            .width('100%').height('100%').backgroundColor('#00CB87') 
        }.tabBar(this.tabBuilder(1, '详情')) 
  
        TabContent() { 
          Column().width('100%').height('100%').backgroundColor('#ffc19757') 
        }.tabBar(this.tabBuilder(2, '我的')) 
  
      } 
      .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%') 
  } 
}

实现效果

适配版本信息

SDK:4.1.0.52

IDE:DevEco Studio 4.1.1.400

分享
微博
QQ
微信
回复
2024-05-24 23:31:39
相关问题
tabs组件 左右滑动延迟较高
447浏览 • 1回复 待解决
如何选择Navigation 组件Tabs 组件
703浏览 • 1回复 待解决
自定义组件嵌套组件
7854浏览 • 3回复 待解决
Tabs组件懒加载的问题
750浏览 • 1回复 待解决
Scroll与WaterFlow滑动嵌套
411浏览 • 1回复 待解决
scroll和list的嵌套滑动
400浏览 • 1回复 待解决
如何自定义模拟Tabs组件
373浏览 • 1回复 待解决
嵌套组件中Scroll不生效
653浏览 • 1回复 待解决
如何实现Tabs组件tarbar的吸顶效果
146浏览 • 1回复 待解决
Tabs组件自定义导航栏UI问题
451浏览 • 1回复 待解决
Swiper 组件嵌套图片刷新数据会闪烁
287浏览 • 1回复 待解决
如何监听List组件的总滑动距离
599浏览 • 1回复 待解决
滑动组件如何实现单边spring的效果
501浏览 • 1回复 待解决
如何去除Tabs组件两侧的蒙层
587浏览 • 1回复 待解决
Tabs组件选中下划线需要平移动效
411浏览 • 1回复 待解决