HarmonyOS TabContent左右切换,上面的Tabs没有跟着滑动

HarmonyOS TabContent左右切换,上面的Tabs没有跟着滑动。

HarmonyOS
2024-10-12 09:50:00
880浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

可以参考以下demo:

@Entry  
@Component  
struct Index {  
  @State tabArray: Array<number> = [0,1,2,3,4,5,6]  
  @State focusIndex: number = 0  
  private controller: TabsController = new TabsController()  
  scrollerForList: Scroller = new Scroller()  
  // 单独的页签  
  @Builder  
  Tab(tabName: string, tabItem: number, tabIndex: number) {  
    Column({ space: 20 }) {  
      Text(tabName).fontSize(18)  
    }  
    .width(100)  
    .height(30)  
    .borderRadius({ topLeft: 10, topRight: 10 })  
    .onClick(() => {  
      this.controller.changeIndex(tabIndex)  
      this.focusIndex = tabIndex  
    })  
    .backgroundColor(tabIndex === this.focusIndex ? "#ffffffff" : "#ffb7b7b7")  
  }  
  build() {  
    Column() {  
      // 页签  
      Row() {  
        List({ scroller: this.scrollerForList }) {  
          ForEach(this.tabArray, (item: number, index: number) => {  
            ListItem() {  
              this.Tab("页" + item, item, index)  
            }  
          })  
        }  
        .listDirection(Axis.Horizontal)  
        .scrollBar(BarState.Off)  
      }  
      .height(30)  
      .justifyContent(FlexAlign.Start)  
      .width('100%')  
      .backgroundColor("#ffb7b7b7")  
  
      //tabs  
      Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {  
        ForEach(this.tabArray, (item: number, index: number) => {  
          TabContent() {  
            Text('我是页面 ' + item + " 的内容")  
              .height(300)  
              .width('100%')  
              .fontSize(30)  
          }  
          .backgroundColor(Color.Blue)  
        })  
      }  
      .barMode(BarMode.Scrollable)  
      .barHeight(0)  
      .animationDuration(100)  
      .onChange((index: number) => {  
        this.focusIndex = index  
        // 使用scroller实现上下联动,具体滚动效果根据业务需要自行设计,以下为滚动示例  
        // 添加第三个参数ScrollAlign.CENTER,指定滑动到的元素与当前容器的对齐方式为居中  
        this.scrollerForList.scrollToIndex(index,true,ScrollAlign.CENTER)  
      })  
    }  
    .alignItems(HorizontalAlign.Start)  
    .width('100%')  
    .height('100%')  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
分享
微博
QQ
微信
回复
2024-10-12 15:36:16
相关问题
tabs组件 左右滑动延迟较高
1539浏览 • 1回复 待解决
HarmonyOS Tabs和Web嵌套左右滑动问题
884浏览 • 1回复 待解决
Tab组件内嵌web view左右滑动切换问题
1063浏览 • 1回复 待解决
HarmonyOS Tabs组件怎么动态添加TabContent
981浏览 • 1回复 待解决
HarmonyOS Tabs组件切换
1073浏览 • 1回复 待解决
HarmonyOS 关于Tabs组件的TabContent问题
660浏览 • 1回复 待解决
Tabs组件TabContent滑到边缘问题
1010浏览 • 0回复 待解决
HarmonyOS tabs切换问题
430浏览 • 1回复 待解决