HarmonyOS Tabs组件使用中,如果tab比较多,滚动时,如何让选中的当前tab自动滚蛋到页面中间展示

Tabs组件使用中,如果tab比较多,滚动时,如何让选中的当前tab自动滚蛋到页面中间展示?

HarmonyOS
2024-12-20 17:11:26
1.5w浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

参考demo:

@Entry
@Component
struct TabsExample {
  @State fontColor: string = '#182431'
  @State selectedFontColor: string = '#007DFF'
  @State currentIndex: number = 0
  private controller: TabsController = new TabsController()

  @Builder
  tabBuilder(index: number, name: string) {
    Column() {
      Text(name)
        .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)
        .fontSize(10)
        .fontWeight(this.currentIndex === index ? 500 : 400)
        .lineHeight(22)
        .margin({ top: 17, bottom: 7 })
      Divider().strokeWidth(2).color('#007DFF').opacity(this.currentIndex === index ? 1 : 0)
    }
    .width('20%')
  }

  build() {
    Column() {
      Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) {

        ForEach(['#F0C808', '#F2B0F5', '#3D1D6E', '#D84E48', '#F8F1A5', '#A4D5E2', '#D5F3C3', '#8B2D4D', '#7D7D7D', '#9A4F4F', '#1A8E7F', '#9E8FA3', '#B7C5D1', '#0D6E4B', '#E3C7C9', '#7B7B7B', '#B7B0D0', '#F0D5F2', '#A7F0C6', '#F1D6C0'], (item: string, index: number) => {
          TabContent() {
            Column().width('100%').height('100%').backgroundColor(item)
          }
          .tabBar(this.tabBuilder(index, item))
        })

      }
      .vertical(false)
      .barMode(BarMode.Scrollable)
      .barWidth(360)
      .barHeight(56)
      .animation( {duration:300})
      .onChange((index: number) => {
        this.currentIndex = index
      })
      .width(360)
      .height(296)
      .margin({ top: 52 })
      .backgroundColor('#F1F3F5')
    }.width('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.
分享
微博
QQ
微信
回复
2024-12-20 19:04:13
相关问题
HarmonyOS Tabs组件Tab滚动问题
1545浏览 • 1回复 待解决
如何获取Scroll组件的当前滚动偏移量
2965浏览 • 1回复 待解决
想实现tabBar多个tab滚动
1092浏览 • 1回复 待解决
Tab组件问题
723浏览 • 1回复 待解决
HarmonyOS Tabs如何设置不自动滚动
593浏览 • 1回复 待解决