思路是从Index进入StartPage广告页面,跳过或时间到了从StartPage跳转到LayouPage页,结果是从Index直接到LayoutPage页Tabs能正常切换,但先跳StartPage,再到LayoutPage就不能正常切换Tabs了
Index.ets代码 :
interface TabsClassType {
icon: ResourceStr;
text: string;
}
@Entry
@Component
struct Index {
pathStack: NavPathStack = new NavPathStack()
build() {
Navigation(this.pathStack) {}
.onAppear(()=>{
this.pathStack.replacePathByName("LayoutPage", null, false);
})
.hideNavBar(true)
}
}
StartPage:
@Builder
export function StartBuilder() {
StartPage();
}
@Component
export struct StartPage {
@State downNumber: number = 3;
pathStack: NavPathStack = new NavPathStack();
timerId: number = 0;
aboutToAppear() {
this.startCountdown();
}
aboutToDisappear() {
this.stopCountdown();
}
startCountdown() {
this.timerId = setInterval(() => {
if (this.downNumber > 1) {
this.downNumber -= 1;
} else {
this.downNumber = 0;
this.stopCountdown();
this.navigateToPageTabs();
}
}, 1000);
}
stopCountdown() {
if (this.timerId > 0) {
clearInterval(this.timerId);
this.timerId = 0;
}
}
navigateToPageTabs() {
this.pathStack.pushPathByName("LayoutPage", null, false);
}
build() {
NavDestination() {
Stack({ alignContent: Alignment.TopEnd }) {
Image($r('app.media.start_ad'))
.width('100%')
.height('100%')
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])
Button(`跳过 ${this.downNumber}`)
.backgroundColor('rgba(0,0,0,0.6)')
.margin(15)
.onClick(() => {
this.stopCountdown();
this.navigateToPageTabs();
})
}
}
.onReady((context: NavDestinationContext) => {
this.pathStack = context.pathStack;
})
}
}
LayoutPage:
interface TabsClassType {
icon: ResourceStr;
text: string;
}
@Builder
export function LayoutBuilder() {
LayoutPage();
}
@Component
export struct LayoutPage {
@State currentIndex: number = 0;
tabsArr: TabsClassType[] = [
{ text: '推荐', icon: $r('app.media.hot_a') },
{ text: '发现', icon: $r('app.media.find_a') },
{ text: '动态', icon: $r('app.media.trend_a') },
{ text: '我的', icon: $r('app.media.my_a') },
]
build() {
NavDestination() {
Tabs({ barPosition: BarPosition.End, index: this.currentIndex }) {
ForEach(this.tabsArr, (item: TabsClassType, index: number) => {
TabContent() {
Text(`页面${index}`)
.fontColor(Color.White)
.fontSize(30)
}
.tabBar(this.tabBuilder(item, index))
.backgroundColor('#131313')
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])
})
}
.backgroundColor('#2b2b2b')
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])
.onChange((index: number) => {
this.currentIndex = index;
})
}
.width('100%')
.height('100%')
}
@Builder
tabBuilder(item: TabsClassType, tabIndex: number) {
Column({ space: 5 }) {
Image(item.icon)
.width(26)
.height(26)
.fillColor(this.currentIndex === tabIndex ? '#00B42A' : '#9c9c9c')
Text(item.text)
.fontSize(14)
.fontColor(this.currentIndex === tabIndex ? '#00B42A' : '#9c9c9c')
}
.padding({ top: 5 })
}
}