Row({ space: 7 }) { Scroll() { Row() { ForEach(this.tabArray, (item: number, index: number) => { this.Tab(`页签 ${item}`, item, index); }) } .justifyContent(FlexAlign.Start) } .align(Alignment.Start) .scrollable(ScrollDirection.Horizontal) .scrollBar(BarState.Off) .width('90%') .backgroundColor('#ffb7b7b7') Image($r('app.media.startIcon')).onClick(() => { if (this.tabArray.length === 0) { this.tabArray = [0]; this.focusIndex = 0; } else { this.tabArray.push(this.tabArray[this.tabArray.length - 1] + 1); this.focusIndex = this.tabArray.length - 1; let add = this.focusIndex; if (add == 1) { this.test = true; } setTimeout(() => { this.controller.changeIndex(add); }, 100); } }).width(20).height(20) } .width('100%') .backgroundColor('#ffb7b7b7')
Tabs({ barPosition: BarPosition.Start, controller: this.controller }) { ForEach(this.tabArray, (item: number) => { TabContent() { Text(`我是页面 ${item} 的内容`) .height(300) .width('100%') .fontSize(30) } }) }
主要是通过TabsController的changeIndex来实现对应的视图跳转,但需注意由于之后会有增删数组元素的操作,所以此处传入的index值是数组元素的索引值。
this.controller.changeIndex(pre - 1);
增添数组元素使用push方法,但由于此demo原始定义的数组是连续的自然数,后续增删数组会打乱原有顺序,所以此处处理为先判断最后一个元素的值再加1,当把所有数组元素删除时,需要再次添加数组元素,同时也需要控制选中样式,所以此基础上加一个判断,让重新生成的页签选中样式在第一个元素上。
if (this.tabArray.length === 0) { this.tabArray = [0]; this.focusIndex = 0; } else { this.tabArray.push(this.tabArray[this.tabArray.length - 1] + 1); this.focusIndex = this.tabArray.length - 1; let add = this.focusIndex; if (add == 1) { this.test = true; } setTimeout(() => { this.controller.changeIndex(add); }, 100); }
let pre = tabIndex; if (this.focusIndex === pre && pre === this.tabArray.length - 1) { this.tabArray.splice(tabIndex, 1); this.focusIndex = pre - 1; this.controller.changeIndex(pre - 1); this.test = true; setTimeout(() => { this.test = false; }, 400); } // 最后一个元素并且当前选中情况 else if (this.focusIndex === pre) { this.tabArray.splice(pre, 1); } // 非最后一个元素且当前选中情况 else if (this.focusIndex > pre) { this.focusIndex = this.focusIndex - 1; this.tabArray.splice(pre, 1); this.controller.changeIndex(this.focusIndex); } // 非当前选中且比选中的小 else { this.tabArray.splice(pre, 1); }
完整示例参考如下:
@Entry @Component struct Drag { @State tabArray: Array<number> = [0, 1]; @State focusIndex: number = 0; private controller: TabsController = new TabsController(); @State test: boolean = false; // 单独的页签 @Builder Tab(tabName: string, tabItem: number, tabIndex: number) { Row({ space: 20 }) { Text(tabName).fontSize(18) Image($r('app.media.startIcon')).width(20).height(20) // 运行时请按需替换图片资源 .onClick(() => { let pre = tabIndex; if (this.focusIndex === pre && pre === this.tabArray.length - 1) { this.tabArray.splice(tabIndex, 1); this.focusIndex = pre - 1; this.controller.changeIndex(pre - 1); this.test = true; setTimeout(() => { this.test = false; }, 400); } // 最后一个元素并且当前选中情况 else if (this.focusIndex === pre) { this.tabArray.splice(pre, 1); } // 非最后一个元素且当前选中情况 else if (this.focusIndex > pre) { this.focusIndex = this.focusIndex - 1; this.tabArray.splice(pre, 1); this.controller.changeIndex(this.focusIndex); } // 非当前选中且比选中的小 else { this.tabArray.splice(pre, 1); } }) } .justifyContent(FlexAlign.Center) .constraintSize({ minWidth: 35 }) .width(120) .height(30) .borderRadius({ topLeft: 10, topRight: 10 }) .backgroundColor(tabIndex === this.focusIndex ? '#ffffffff' : '#ffb7b7b7') .onClick(() => { this.test = true; this.focusIndex = tabIndex; this.controller.changeIndex(tabIndex); console.info(`foo ${tabItem}`); }) } build() { Column() { Column() { Row({ space: 7 }) { Scroll() { Row() { ForEach(this.tabArray, (item: number, index: number) => { this.Tab(`页签 ${item}`, item, index); }) } .justifyContent(FlexAlign.Start) } .align(Alignment.Start) .scrollable(ScrollDirection.Horizontal) .scrollBar(BarState.Off) .width('90%') .backgroundColor('#ffb7b7b7') Image($r('app.media.startIcon')).onClick(() => { if (this.tabArray.length === 0) { this.tabArray = [0]; this.focusIndex = 0; } else { this.tabArray.push(this.tabArray[this.tabArray.length - 1] + 1); this.focusIndex = this.tabArray.length - 1; let add = this.focusIndex; if (add == 1) { this.test = true; } setTimeout(() => { this.controller.changeIndex(add); }, 100); } }).width(20).height(20) } .width('100%') .backgroundColor('#ffb7b7b7') Tabs({ barPosition: BarPosition.Start, controller: this.controller }) { ForEach(this.tabArray, (item: number) => { TabContent() { Text(`我是页面 ${item} 的内容`) .height(300) .width('100%') .fontSize(30) } }) } .onChange((index: number) => { if (index !== 0 && this.tabArray.length > 2) { this.focusIndex = index; console.info(`change focusIndex ${this.focusIndex}`); } else if (this.tabArray.length == 1) { this.focusIndex = index; } else if (this.tabArray.length == 2 && this.focusIndex == 1) { if (this.test) { this.focusIndex = 1; } else { this.focusIndex = 0; } } else if (!this.test) { console.info(`foo ${index}`); this.focusIndex = index; } }) } .alignItems(HorizontalAlign.Start) .width('100%') } .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM]) .backgroundColor('#ffebebeb') .height('100%') } }