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%') 
  } 
}
  • 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.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.

实现效果

适配版本信息

SDK:4.1.0.52

IDE:DevEco Studio 4.1.1.400

分享
微博
QQ
微信
回复
2024-05-24 23:31:39
相关问题
HarmonyOS Tabs组件嵌套滑动
1178浏览 • 1回复 待解决
HarmonyOS Tabs组件嵌套Tabs组件问题
1777浏览 • 1回复 待解决
tabs组件 左右滑动延迟较高
1560浏览 • 1回复 待解决
HarmonyOS Refresh组件嵌套滑动冲突问题
1942浏览 • 1回复 待解决
如何处理tabs嵌套web滑动场景
1220浏览 • 1回复 待解决
HarmonyOS Tabs和Web嵌套左右滑动问题
917浏览 • 1回复 待解决
HarmonyOS Tabs组件组件问题
1400浏览 • 1回复 待解决
自定义组件嵌套组件
10478浏览 • 3回复 待解决