问题现象
Tabs组件嵌套Web组件在滚动场景中可能出现以下问题:
解决措施
示例代码如下:
import { webview } from '@kit.ArkWeb';
@Component
@Entry
struct TabWebScroll {
@State isScrollEnabled: boolean = true; // Control the sliding page for page switching
private tabsController = new TabsController();
private currentIndex: number = 0;// Track currently active tab index
private webviewController: webview.WebviewController = new webview.WebviewController();
build() {
Tabs({ barPosition: BarPosition.End, controller: this.tabsController }) {
TabContent() {
Web({ src: 'https://developer.huawei.com/consumer/cn/', controller: this.webviewController })
.nestedScroll({
// Set nested scrolling
scrollForward: NestedScrollMode.PARENT_FIRST,
scrollBackward: NestedScrollMode.SELF_FIRST
})
}
.tabBar(this.tabBuilder('home page', 0))
TabContent() {
Column() {
Text('find')
}
.width('100%')
.height('100%')
}
.tabBar(this.tabBuilder('find', 1))
TabContent() {
Column() {
Text('recommend')
}
.width('100%')
.height('100%')
}
.tabBar(this.tabBuilder('recommend', 2))
TabContent() {
Column() {
Text('my')
}
.width('100%')
.height('100%')
}
.tabBar(this.tabBuilder('my', 3))
}
.onChange((index: number) => {
this.currentIndex = index;
})
.scrollable(this.isScrollEnabled)
.onAnimationEnd(() => {
// Trigger this callback when the animation ends, and set the web component to slide
this.webviewController?.setScrollable(true);
})
.onGestureSwipe(() => {
// During the sliding process on the page, this callback is triggered frame by frame. When switching between tab pages, the web page cannot slide up or down
this.webviewController?.setScrollable(false);
})
}
@Builder
tabBuilder(title: string, targetIndex: number) {
Column() {
Text(title)
.fontColor(this.currentIndex === targetIndex ? '#1698CE' : '#6B6B6B')
}
.width('100%')
.height(50)
.justifyContent(FlexAlign.Center)
}
}