HarmonyOS 自定义的TabItem的坐标怎么获取?

我用Scroll实现了一个自定义的滚动的TabBar,现在有个问题就是我在切换页面的时候,Scroll没有办法把不可见的TabItem滑动到屏幕的中间。

Scroll滚动都是需要坐标的,自定义的TabItem的坐标怎么获取?在Scroll中怎么实现这个系统自带的TabBar跟随TabContent滚动的效果?

HarmonyOS
2024-09-26 11:26:51
837浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

Scroll滚动都是需要坐标的,自定义的TabItem的坐标怎么获取?可参考如下代码:

export class TabBarDataType {  
  id: number;  
  selectedIcon: ResourceStr;  
  defaultIcon: ResourceStr;  
  
  constructor(id: number, selectedIcon: ResourceStr, defaultIcon: ResourceStr) {  
    this.id = id;  
    this.selectedIcon = selectedIcon;  
    this.defaultIcon = defaultIcon;  
  }  
}  
  
export const TABINFO: TabBarDataType[] = [  
  new TabBarDataType(0, $r('app.media.ic_product01'), $r('app.media.ic_product_normal')),  
  new TabBarDataType(1, $r('app.media.ic_product01'), $r('app.media.ic_product_normal')),  
  new TabBarDataType(2, $r('app.media.ic_product01'), $r('app.media.ic_product_normal')),  
  new TabBarDataType(3, $r('app.media.ic_product01'), $r('app.media.ic_product_normal')),  
  new TabBarDataType(4, $r('app.media.ic_product01'), $r('app.media.ic_product_normal')),  
  new TabBarDataType(5, $r('app.media.ic_product01'), $r('app.media.ic_product_normal')),  
  
];  
  
@Entry  
@Component  
struct TabViewDemo {  
  @Provide selectedIndex: number = 0; // 初始化被选定的tabBar下标  
  private controller: TabsController = new TabsController(); // 初始化Tab控制器  
  
  build() {  
    Column() {  
      // 自定义TabBar组件  
      CustomTabBar({ selectedIndex: $selectedIndex })  
      Tabs({ index: this.selectedIndex, barPosition: BarPosition.End, controller: this.controller }) {  
        ForEach(TABINFO, (item: TabBarDataType, tabIndex: number) => {  
          // 单独一个TabBar组件  
          TabContent() {  
            Text("tab" + item.id).fontSize(26)  
          }  
        })  
      }  
      .vertical(false)  
      .scrollable(true)  
      .layoutWeight(1)  
      .backgroundColor('#ffdbd9d9')  
      .barHeight(0)  
      .onChange((index: number) => {  
        this.selectedIndex = index;  
      })  
    }.width("100%").height("100%")  
  }  
}  
  
@Component  
struct CustomTabBar {  
  @Link @Watch("scrollTo") selectedIndex: number; // 初始化被选定的tabBar下标  
  private scrollerForScroll: Scroller = new Scroller()  
  private scrollerForList: Scroller = new Scroller()  
  
  scrollTo() {  
    this.scrollerForList.scrollToIndex(this.selectedIndex, false, ScrollAlign.CENTER)  
  }  
  
  build() {  
    Scroll() {  
      Row() {  
        List({ space: 20, scroller: this.scrollerForList }) {  
          ForEach(TABINFO, (item: TabBarDataType, tabIndex: number) => {  
            // 单独一个TabBar组件  
            TabItem({  
              tabBarIndex: tabIndex,  
              selectedIndex: $selectedIndex,  
            })  
          })  
        }.width('100%').listDirection(Axis.Horizontal)  
        .scrollBar(BarState.Off)  
      }  
    }  
    .scrollBar(BarState.Off)  
    .scrollable(ScrollDirection.Horizontal)  
    .height(60)  
  }  
}  
  
@Component  
struct TabItem {  
  @Prop tabBarIndex: number; // tabBar下标  
  @Link selectedIndex: number; // 初始化被选定的tabBar下标  
  
  build() {  
    Column() {  
      Column() {  
        // 通过被选中的tabBar下标值和tabBar的默认下标值来改变图片显示  
        Image(this.selectedIndex === this.tabBarIndex ? TABINFO[this.tabBarIndex].selectedIcon :  
        TABINFO[this.tabBarIndex].defaultIcon)  
          .interpolation(ImageInterpolation.High)  
          .size(  
            { width: 28, height: 28 })  
      }  
      .width(37)  
      .height(37)  
      .justifyContent(FlexAlign.Center)  
    }  
    .width(60)  
    .onClick(() => {  
      // 更新被选中的tabBar下标  
      this.selectedIndex = this.tabBarIndex;  
    })  
  }  
}
  • 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.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.

在Scroll中怎么实现这个系统自带的TabBar跟随TabContent滚动的效果?

可参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-tabs-V5#%E7%A4%BA%E4%BE%8B9

分享
微博
QQ
微信
回复
2024-09-26 17:01:56
相关问题
HarmonyOS 怎么自定义TabTabbar
716浏览 • 1回复 待解决
自定义弹框状态获取
1577浏览 • 1回复 待解决
HarmonyOS 如何获取自定义组件高度?
1224浏览 • 1回复 待解决
鸿蒙怎么实现自定义布局Dialog
10085浏览 • 2回复 已解决
HarmonyOS 怎么自定义装饰器
743浏览 • 1回复 待解决
如何获取用户设备自定义名字?
255浏览 • 0回复 待解决
HarmonyOS 自定义组件如何获取高度?
1041浏览 • 1回复 待解决
HarmonyOS 自定义绘制接口怎么使用
646浏览 • 1回复 待解决