在实现侧边栏时,希望侧边栏从最顶部开始往下排列,在条目撑满侧边栏时可以滑动,且在选中条目时能自动居中。但是使用Tabs组件中把页签设置在侧边,页签中的条目始终是居中排列,无法做到从最顶部开始往下排列。
由于Tabs组件无法实现自顶到底的效果,可以通过自定义组件来达到此效果。实现如下:
@Entry @Component struct Q2025040100041 { @State currentIndex: number = 0; tabBarArr: string[] = ['tab0', 'tab1', 'tab2', 'tab3', 'tab4', 'tab5', 'tab6', 'tab7', 'tab8', 'tab9']; listScroller: Scroller = new Scroller(); tabsController: TabsController = new TabsController(); build() { Row() { List({ scroller: this.listScroller }) { ForEach(this.tabBarArr, (item: string, index: number) => { ListItem() { Text(item) .fontColor(index === this.currentIndex ? "#0a59f7" : "#808080") .textAlign(TextAlign.Center) .height('100vp') .width('100vp'); } .onClick(() => { this.currentIndex = index; this.listScroller.scrollToIndex(this.currentIndex, true, ScrollAlign.CENTER); this.tabsController.changeIndex(this.currentIndex); }); }); } .scrollBar(BarState.Off) .width('100vp') .height('100%'); Tabs({ controller: this.tabsController, barPosition: BarPosition.Start }) { ForEach(this.tabBarArr, (item: string) => { TabContent() { Text(item).fontSize('30fp'); }; }); } .barWidth(0) .backgroundColor("#f1f3f5") .onChange((index: number) => { this.currentIndex = index; this.listScroller.scrollToIndex(this.currentIndex, true, ScrollAlign.CENTER); }) .vertical(true) .width('calc(100% - 100vp)') .height('100%'); } .alignItems(VerticalAlign.Top) .width('100%') .height('100%'); } }