HarmonyOS 关于自定义tab点击滑动相关问题

如何解决ListItem的高度较低时,切换tab不能精准定位问题,而且最后一个tab点击无法高亮显示。

TabBar超出屏幕宽度时,如何点击屏幕栏边缘tab时自动滑动tab栏。

HarmonyOS
2024-09-03 09:23:10
819浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

您可以根据自身业务来调整list的item滑动到某个top的某个范围时,显示对应的tabbar。

.onAreaChange((oldValue: Area, newValue: Area) => { 
  if (this.currentIndex === index) { 
    // 这里的距离根据自身业务来调整 
    let posY = Number(newValue.position.y || 0) 
    let offsetY = Number(newValue.width) / 3 
    if (posY>= 0 && posY< offsetY) { 
      this.indicatorIndex = this.currentIndex 
    } 
  } 
})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

参考demo:

import curves from '@ohos.curves'; 
import display from '@ohos.display'; 
import componentUtils from '@ohos.ArkUI.componentUtils'; 
 
class ItemClass { 
  content: string = ''; 
  color: Color = Color.White; 
} 
 
@Entry 
@Component 
struct SwiperTab { 
  private displayInfo: display.Display | null = null; 
  private listScroller: ListScroller = new ListScroller(); 
  private animationDuration: number = 300; //动画时间 
  private animationCurve: ICurve = curves.interpolatingSpring(7, 1, 328, 34); // 动画曲线 
  @State indicatorIndex: number = 0; //当前页签Index 
  private scroller: Scroller = new Scroller(); 
  private barArr: string[] = ['关注', '推荐', '热点', '上海', '视频', '新时代', '新歌', '新碟', '新片']; 
  @State currentIndex: number = 0; 
  @State endIndex: number = 0; 
  private arrList: ItemClass[] = []; 
  private colorArr: Color[] = [Color.White, Color.Blue, Color.Brown, Color.Green, Color.Gray]; 
  @State isClickBar: boolean = false 
 
  aboutToAppear(): void { 
    //获取屏幕实例 
    this.displayInfo = display.getDefaultDisplaySync(); 
    for (let i = 0; i < 9; i++) { 
      let data: ItemClass = { 
        content: i.toString(), 
        color: this.colorArr[i % 5] 
      } 
      this.arrList.push(data); 
    } 
  } 
 
  // 获取屏幕宽度,单位vp 
  private getDisplayWidth(): number { 
    return this.displayInfo != null ? px2vp(this.displayInfo.width) : 0; 
  } 
 
  // 获取组件大小、位置、平移缩放旋转及仿射矩阵属性信息。 
  private getTextInfo(index: number): Record<string, number> { 
    let modePosition: componentUtils.ComponentInfo = componentUtils.getRectangleById(index.toString()); 
    try { 
      return { 'left': px2vp(modePosition.windowOffset.x), 'width': px2vp(modePosition.size.width) } 
    } catch (error) { 
      return { 'left': 0, 'width': 0 } 
    } 
  } 
 
  private scrollIntoView(currentIndex: number): void { 
    const indexInfo = this.getTextInfo(currentIndex); 
    let tabPositionLeft = indexInfo.left; 
    let tabWidth = indexInfo.width; 
    // 获取屏幕宽度,单位vp 
    const screenWidth = this.getDisplayWidth(); 
    //当前滚动的偏移量 
    const currentOffsetX: number = this.scroller.currentOffset().xOffset; 
 
    // 将页签bar定位在正中间 
    this.scroller.scrollTo({ 
      xOffset: currentOffsetX + tabPositionLeft - screenWidth / 2 + tabWidth / 2, 
      yOffset: 0, 
      animation: { 
        duration: this.animationDuration, 
        // 动画曲线 
        curve: this.animationCurve, 
      } 
    }); 
  } 
 
  private startAnimateTo(duration: number, marginLeft: number, width: number): void { 
    animateTo({ 
      duration: duration, 
      // 动画曲线 
      curve: this.animationCurve, 
      onFinish: () => { 
      } 
    }, () => { 
    }) 
  } 
 
  // 下划线动画 
  private underlineScrollAuto(duration: number, index: number): void { 
    let indexInfo = this.getTextInfo(index); 
    this.startAnimateTo(duration, indexInfo.left, indexInfo.width); 
  } 
 
  build() { 
    Column() { 
      Column() { 
        Scroll(this.scroller) { 
          Row() { 
            ForEach(this.barArr, (item: string, index: number) => { 
              Column() { 
                Text(item) 
                  .fontSize(16) 
                  .borderRadius(5) 
                  .fontColor(this.indicatorIndex === index ? Color.Red : Color.Black) 
                  .fontWeight(this.indicatorIndex === index ? FontWeight.Bold : FontWeight.Normal) 
                  .margin({ left: 5, right: 5 }) 
                  .padding({ left: 8, right: 8 }) 
                  .id(index.toString()) 
                  .onClick(() => { 
                    this.indicatorIndex = index; 
                    this.isClickBar = true 
                    this.scrollIntoView(index); 
                    // 跟List进行联动 
                    this.listScroller.scrollToIndex(index) 
                  }) 
                  .border(this.indicatorIndex == index ? { 
                    width: { 
                      left: 0, 
                      right: 0, 
                      top: 0, 
                      bottom: 2 
                    }, 
                    color: { bottom: Color.Red }, 
                    radius: 0, 
                    style: { 
                      bottom: BorderStyle.Solid, 
                    } 
                  } : null) 
              } 
            }, (item: string) => item) 
          } 
          .height(32) 
        } 
        .width('100%') 
        .scrollable(ScrollDirection.Horizontal) 
        .scrollBar(BarState.Off) 
        .edgeEffect(EdgeEffect.None) 
        .onScrollStop(() => { 
          this.underlineScrollAuto(0, this.indicatorIndex); 
        }) 
      } 
      .width('90%') 
      .margin({ top: 15, bottom: 10 }) 
 
      List({ space: 10, scroller: this.listScroller }) { 
        ForEach(this.arrList, (item: ItemClass, index: number) => { 
          ListItem() { 
            Column() { 
              Text(item.content) 
            } 
            .width('100%') 
            .height(200) 
            .backgroundColor(item.color) 
          } 
          .onAreaChange((oldValue: Area, newValue: Area) => { 
            if (this.currentIndex === index) { 
              // 这里的距离根据自身业务来调整 
              let posY = Number(newValue.position.y || 0) 
              let offsetY = Number(newValue.width) / 3 
              if (posY >= 0 && posY < offsetY) { 
                this.indicatorIndex = this.currentIndex 
              } 
            } 
          }) 
        }, (item: ItemClass) => item.content) 
      } 
      .edgeEffect(EdgeEffect.None) 
      .scrollBar(BarState.Off) 
      .onScrollIndex((start: number, end: number, center: number) => { 
        this.currentIndex = start 
        this.endIndex = end 
      }) 
      .onReachEnd(() => { 
        // 处理最后一个页签 
        if (!this.isClickBar) { 
          this.indicatorIndex = this.endIndex 
        } 
      }) 
      .onScrollStop(() => { 
        this.scrollIntoView(this.indicatorIndex); 
      }) 
      .onScrollFrameBegin((offset: number, state: ScrollState) => { 
        this.isClickBar = false 
        return { offsetRemain:offset } 
      }) 
      .width("100%") 
      .height("calc(100% - 50vp)") 
      .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.
  • 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.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
分享
微博
QQ
微信
回复
2024-09-03 17:33:37
相关问题
HarmonyOS 自定义dialog相关问题
746浏览 • 1回复 待解决
HarmonyOS 如何自定义tab
1447浏览 • 2回复 待解决
自定义弹窗使用相关问题
1730浏览 • 1回复 待解决
HarmonyOS 自定义弹窗点击跳转问题
789浏览 • 1回复 待解决
HarmonyOS 自定义滑动组件
694浏览 • 1回复 待解决
HarmonyOS 自定义滑动
651浏览 • 1回复 待解决
HarmonyOS 怎么自定义Tab的Tabbar
716浏览 • 1回复 待解决
HarmonyOS Slider滑动自定义tips
920浏览 • 1回复 待解决
关于自定义的XComponent加载so的问题
910浏览 • 1回复 待解决
HarmonyOS 关于自定义相机功能
993浏览 • 1回复 待解决
HarmonyOS UI组件自定义点击范围
1189浏览 • 1回复 待解决
HarmonyOS 关于自定义协议跳转APP
832浏览 • 1回复 待解决
HarmonyOS 自定义通知栏点击更新需求
627浏览 • 1回复 待解决
HarmonyOS 自定义组件问题
1365浏览 • 1回复 待解决