IT评测·应用市场-qidao123.com技术社区

标题: HarmonyOS中实现TabBar(相称于Android中的TabLayout+ViewPager) [打印本页]

作者: 小秦哥    时间: 2025-1-11 06:22
标题: HarmonyOS中实现TabBar(相称于Android中的TabLayout+ViewPager)
 参考网址:自定义页签切换联动
1.自定义组件TabBarView

  1. @Component
  2. export struct TabBarView{
  3.   @State currentIndex: number = 0
  4.   @State selectedIndex: number = 0
  5.   private controller: TabsController = new TabsController()
  6.   //tab标签内容+横线布局
  7.   @Builder tabBuilder(index: number, name: string) {
  8.     Column() {
  9.       Text(name)
  10.         .fontColor(this.selectedIndex === index ? '#007DFF' : '#182431')
  11.         .fontSize(18)
  12.         .fontWeight(this.selectedIndex === index ? 500 : 400)
  13.         .lineHeight(22)
  14.         .height('95%')
  15.       Divider()
  16.         .strokeWidth(2)
  17.         .color('#007DFF')
  18.         .opacity(this.selectedIndex === index ? 1 : 0)
  19.     }
  20.     .backgroundColor(Color.White)
  21.     .width('100%')
  22.     .height('100%')
  23.   }
  24.   @Link arr: Array<string>
  25.   @Builder tabItem(item: string) {}
  26.   @BuilderParam item: (item: string) => void = this.tabItem
  27.   build() {
  28.     Column() {
  29.       Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) {
  30.         ForEach(this.arr, (item: string, index: number) => {
  31.           TabContent() {
  32.             this.item(item)
  33.           }.tabBar(this.tabBuilder(index, item))
  34.         }, (item: string, index: number) => index.toString())
  35.       }
  36.       .vertical(false)
  37.       .barMode(BarMode.Fixed)
  38.       .barWidth(360)
  39.       .barHeight(56)
  40.       .animationDuration(400)
  41.       .onChange((index: number) => {
  42.         // currentIndex控制TabContent显示页签
  43.         this.currentIndex = index
  44.         this.selectedIndex = index
  45.       })
  46.       .onAnimationStart((index: number, targetIndex: number, event: TabsAnimationEvent) => {
  47.         if (index === targetIndex) {
  48.           return
  49.         }
  50.         // selectedIndex控制自定义TabBar内Image和Text颜色切换
  51.         this.selectedIndex = targetIndex
  52.       })
  53.       .width('100%')
  54.       .height('100%')
  55.       .backgroundColor('#F1F3F5')
  56.     }.width('100%')
  57.   }
  58. }
复制代码
2.组件中使用

  1. import { TabBarView } from "./TabBarView"
  2. @Entry({routeName: 'IndexPage'})
  3. @Component
  4. export struct IndexPage{
  5.   @State arr: Array<string> = ['标签1', '标签2']
  6.   @Builder
  7.   tabItem(item: string){
  8.     //在此处写各个标签对于的布局组件,可抽离出来使代码简洁
  9.     if (item === this.arr[0]){
  10.       //标签1组件
  11.     }else if (item === this.arr[1]){
  12.       //标签2组件
  13.     }
  14.   }
  15.   build() {
  16.     Column(){
  17.       TabBarView({arr: this.arr, item: this.tabItem})
  18.     }
  19.   }
  20. }
复制代码
说明:我是根据官网提供的Tabs实例代码1(自定义页签切换联动)封装的简单使用(代码可直接使用),这种方式是为了复用。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4