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

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

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

HarmonyOS
2024-09-03 09:23:10
浏览
收藏 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 
    } 
  } 
})

参考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%') 
  } 
}
分享
微博
QQ
微信
回复
2024-09-03 17:33:37
相关问题
自定义弹窗使用相关问题
634浏览 • 1回复 待解决
HarmonyOS 自定义弹窗的问题
234浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog问题
178浏览 • 1回复 待解决
HarmonyOS 自定义Dialog显示问题
105浏览 • 1回复 待解决
HarmonyOS TextInput自定义键盘问题
168浏览 • 1回复 待解决
HarmonyOS TextInput绑定自定义键盘问题
174浏览 • 1回复 待解决
HarmonyOS 引用自定义web的模块问题
76浏览 • 1回复 待解决
自定义弹窗自定义转场动画
664浏览 • 1回复 待解决
HarmonyOS 自定义Dialog背景色透明问题
246浏览 • 1回复 待解决
自定义装饰器的使用问题
469浏览 • 1回复 待解决
HarmonyOS关于混淆强度相关问题
182浏览 • 1回复 待解决
HarmonyOS 如何自定义BuildMode?
112浏览 • 1回复 待解决
自定义参数BuildProfile的问题汇总
793浏览 • 1回复 待解决
HarmonyOS 如何设置自定义字体
317浏览 • 1回复 待解决