如何优雅设置tabs组件默认定位tabbar
tabs组件的tabbar, 不想默认是第0个,想自定义默认值。没有找到要设置的属性。
图中,不想默认定位到视频,可自己选择默认是哪个tabbar.
创建tabs的代码:
build(){
Column() {
Scroll() {
Row(){
ForEach(this.subTabDataArray,(item:WorkSubtabModel,index:number) =>
{
Row(){
this.WorkSubTabCellView(item,index)
}.onClick(()=> {
this.currentIndex = index
this.controller.changeIndex(index)
})
})
}.margin({left:16,right:6,top:14,bottom:14})
}.scrollable(ScrollDirection.Horizontal)
.scrollBar(BarState.Off)
.width('100%')
.align(Alignment.Start)
Tabs({barPosition:BarPosition.Start,index:1,controller:this.controller}){
ForEach(this.subTabDataArray,(subtabModel:WorkSubtabModel,index:number)=>{
TabContent() {
if(subtabModel.collectId == -1) {
//视频
WorkVideoPage({uid: this.uid,viewModel:this.viewModel})
}else if(subtabModel.collectId == -2){
//图片
WorkPicturePage({uid: this.uid,viewModel:this.viewModel})
}else if(subtabModel.collectId == -3) {
//播单
Column() {
Text('播单')
}.backgroundColor($r('app.color.c_fdd2d2'))
}else if(subtabModel.collectId == -4) {
//剧集
Column() {
Text('剧集')
}.backgroundColor($r('app.color.c_5172FF'))
}
else if(subtabModel.collectId == -6) {
//音频
WorkAudioPage({uid: this.uid,viewModel:this.viewModel})
}else {
//单个作品集
SingleWorkPage({uid: this.uid, viewModel: new SingleWorkViewModel(subtabModel,this.uid)})
}
}
})
}.vertical(false)
.scrollable(false)
.barHeight(0)
.height('100%')
.width('100%')
}
}
您可以使用index属性来设置默认展示的页面,参考链接:
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-tabs-V5\#接口