@Entry
@Component
struct MultiTabs {
@State currentIndex: number = 1;
@State subCurrentIndex: number = 0;
private fontColor: string = '#ff4e5f60';
private selectedFontColor: string = '#0A59F7';
private controller: TabsController = new TabsController();
private subController: TabsController = new TabsController();
private panOptionLeft: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Left });
private panOptionRight: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Right });
@Builder
tabBuilder(index: number, name: string) {
Column() {
Text(name)
.width('100%')
.height('36vp')
.textAlign(TextAlign.Center)
.fontColor(this.currentIndex === index ? Color.Black : this.fontColor)
.fontSize(16)
.fontWeight(this.currentIndex === index ? 500 : 400)
.lineHeight(22)
.backgroundColor(this.currentIndex === index ? Color.White : '#E5E5EA')
.borderRadius('50vp');
}
.backgroundColor('#E5E5EA')
.borderRadius({
topLeft: index == 0 ? 50 : 0,
bottomLeft: index == 0 ? 50 : 0,
topRight: index == 2 ? 50 : 0,
bottomRight: index == 2 ? 50 : 0
})
.margin({ top: 5 })
.padding({
left: 2,
right: 2,
top: 2,
bottom: 2
});
}
@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);
Divider()
.width('50vp')
.strokeWidth(2)
.color(this.selectedFontColor)
.opacity(this.subCurrentIndex === index ? 1 : 0);
}
.width('80%');
}
build() {
Column() {
// 一级Tab
Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) {
TabContent() {
Column() {
Text('首页内容');
}
.width('100%')
.height('100%')
.borderRadius(12)
.backgroundColor('#F1F3F5')
.justifyContent(FlexAlign.Center)
.alignItems(HorizontalAlign.Center);
}
.tabBar(this.tabBuilder(0, '首页'))
.margin({ left: 16, right: 16, top: 5 });
TabContent() {
Column() {
Tabs({ barPosition: BarPosition.Start, controller: this.subController }) {
TabContent() {
Column().width('100%').height('100%').backgroundColor('#F1F3F5').borderRadius(12);
}
.tabBar(this.subTabBuilder(0, '组件1'))
.margin({ left: 16, right: 16 })
// 关键代码
.gesture(
PanGesture(this.panOptionRight)
.onActionEnd(() => {
this.controller.changeIndex(0);
})
);
TabContent() {
Column().width('100%').height('100%').backgroundColor('#F1F3F5').borderRadius(12);
}
.tabBar(this.subTabBuilder(1, '组件2'))
.margin({ left: 16, right: 16, top: 5 });
TabContent() {
Column().width('100%').height('100%').backgroundColor('#F1F3F5').borderRadius(12);
}
.tabBar(this.subTabBuilder(2, '组件3'))
.margin({ left: 16, right: 16, top: 5 });
TabContent() {
Column().width('100%').height('100%').backgroundColor('#F1F3F5').borderRadius(12);
}
.tabBar(this.subTabBuilder(3, '组件4'))
.margin({ left: 16, right: 16, top: 5 })
// 关键代码
.gesture(
PanGesture(this.panOptionLeft)
.onActionEnd(() => {
this.controller.changeIndex(2);
})
);
}
.animationDuration(400)
.onChange((index: number) => {
this.subCurrentIndex = index;
})
.width('100%');
}
.width('100%').height('100%');
}.tabBar(this.tabBuilder(1, '详情'));
TabContent() {
Column() {
Text('我的内容');
}
.width('100%')
.height('100%')
.borderRadius(12)
.backgroundColor('#F1F3F5')
.justifyContent(FlexAlign.Center)
.alignItems(HorizontalAlign.Center);
}
.tabBar(this.tabBuilder(2, '我的'))
.margin({ left: 16, right: 16, top: 5 });
}
.barWidth('90%')
.onChange((index: number) => {
this.currentIndex = index;
})
.height(296);
};
}
} 可以使用手势拦截增强解决Tabs多层嵌套滑动冲突问题。使用示例请参考嵌套场景下拦截内部容器手势,通过onGestureRecognizerJudgeBegin监听手势事件,内层Tabs到头/尾时拒绝手势传递,允许外层Tabs响应。