HarmonyOS tab嵌套横向list,如何实现list滑动到最右边就自动触发tab的滑动切换?

tab嵌套横向list, tab的左右滑动与横向list冲突。如何解决?


HarmonyOS
2024-10-17 09:40:26
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

可以参考以下代码实现list滑动到最右边就自动触发tab的滑动切换:

import { router } from '@kit.ArkUI'  
@Entry  
@Component  
export struct Tabs240529160413047 {  
  @State tabArray: Array<number> = [0, 1, 2, 3, 4, 5, 6, 7]  
  @State focusIndex: number = 0  
  @State pre: number = 0  
  @State index: number = 0  
  private controller: TabsController = new TabsController()  
  @State test: boolean = false  
  @State animationDuration: number = 300  
  @State indicatorLeftMargin: number = 0  
  @State indicatorWidth: number = 0  
  private tabWidth: number = 0;  
  private scrollerForScroll: Scroller = new Scroller()  
  // 单独的页签  
  @Builder  
  Tab(tabName: string, tabItem: number, tabIndex: number) {  
    Row({ space: 20 }) {  
      Text(tabName)  
        .fontSize(18)  
        .fontColor(tabIndex === this.focusIndex ? Color.Blue : Color.Black)  
        .id(tabIndex.toString())  
        .onAreaChange((oldValue: Area, newValue: Area) => {  
          if (this.focusIndex === tabIndex && (this.indicatorLeftMargin === 0 || this.indicatorWidth === 0)) {  
            if (newValue.position.x != undefined) {  
              let positionX = Number.parseFloat(newValue.position.x.toString())  
              this.indicatorLeftMargin = Number.isNaN(positionX) ? 0 : positionX  
            }  
            let width = Number.parseFloat(newValue.width.toString())  
            this.tabWidth = Number.isNaN(width) ? 0 : width  
            this.indicatorWidth = this.tabWidth  
          }  
        })  
    }  
    .justifyContent(FlexAlign.Center)  
    .constraintSize({ minWidth: 35 })  
    .width(120)  
    .height(30)  
    .onClick(() => {  
      this.controller.changeIndex(tabIndex)  
      this.focusIndex = tabIndex  
    })  
    .backgroundColor("#ffb7b7b7")  
  }  
  @Builder  
  WebTab(tabName: string, tabItem: number, tabIndex: number) {  
    Row({ space: 20 }) {  
      Text(tabName).fontSize(18).fontColor(tabIndex === this.focusIndex ? Color.Blue : Color.Black)  
    }  
    .justifyContent(FlexAlign.Center)  
    .constraintSize({ minWidth: 35 })  
    .width(120)  
    .height(30)  
    .onClick(() => {   
      router.pushUrl({ url: 'pages/WebPage' })  
    })  
    .backgroundColor("#ffb7b7b7")  
  }  
  build() {  
    Column() {  
      Stack({ alignContent: Alignment.TopStart }) {  
        Row() { // 页签  
          List({ space: 0, initialIndex: 0, scroller: this.scrollerForScroll }) {  
            ForEach(this.tabArray, (item: number, index: number) => {  
              if (item === 4) {  
                ListItem() {  
                  this.WebTab("页签 " + item, item, index)  
                }  
              } else {  
                ListItem() {  
                  this.Tab("页签 " + item, item, index)  
                }  
              }  
            }, (item: string) => item)  
          }  
          .listDirection(Axis.Horizontal)  
          .height(30)  
          .friction(0.6)  
          .alignListItem(ListItemAlign.Start)  
          .scrollBar(BarState.Off)  
          .width('100%')  
          .backgroundColor("#ffb7b7b7")  
          .onScroll((xOffset: number, yOffset: number) => {  
            this.indicatorLeftMargin -= xOffset  
          })  
        }.width('100%')  
  
        Column()  
          .height(2)  
          .width(this.indicatorWidth)  
          .margin({ left: this.indicatorLeftMargin, top: 30 })  
          .backgroundColor(Color.Blue)  
      }  
      .height(40)  
      .width('100%')  
      .backgroundColor("#ffb7b7b7") //tabs  
      Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {  
        ForEach(this.tabArray, (item: number, index: number) => {  
          TabContent() {  
            Text('我是页面 ' + item + " 的内容").height(300).width('100%').fontSize(30)  
          }.backgroundColor(Color.White)  
        }, (item: string) => item)  
      }.width('100%').barHeight(0).animationDuration(0).onChange((index: number) => {  
        console.log('foo change')  
        this.focusIndex = index  
        this.scrollerForScroll.scrollToIndex(index - 1, true)  
        let indexInfo = this.getTextInfo(index)  
        let currentLeft = indexInfo.left  
        let currentWidth = indexInfo.width  
        this.startAnimateTo(20, currentLeft, currentWidth)  
      })  
    }.width('100%').height('100%')  
  }  
  private getTextInfo(index: number): Record<string, number> {  
    let strJson = getInspectorByKey(index.toString())  
    try {  
      let obj: Record<string, string> = JSON.parse(strJson)  
      let rectInfo: number[][] = JSON.parse('[' + obj.$rect + ']')  
      return { 'left': px2vp(rectInfo[0][0]), 'width': px2vp(rectInfo[1][0] - rectInfo[0][0]) }  
    } catch (error) {  
      return { 'left': 0, 'width': 0 }  
    }  
  }  
  private startAnimateTo(duration: number, leftMargin: number, width: number) {  
    animateTo({  
      duration: duration, // 动画时长  
      curve: Curve.Linear, // 动画曲线  
      iterations: 1, // 播放次数  
      playMode: PlayMode.Normal, // 动画模式  
      onFinish: () => {  
        console.info('play end')  
      }  
    }, () => {  
      this.indicatorLeftMargin = leftMargin  
      this.tabWidth = width  
      this.indicatorWidth = width  
    })  
  }  
}
  • 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.
分享
微博
QQ
微信
回复
2024-10-17 17:53:55


相关问题
HarmonyOS list 嵌套web滑动切换问题
1224浏览 • 1回复 待解决
HarmonyOS list嵌套tab中列表高度变化
702浏览 • 1回复 待解决
scroll和list嵌套滑动
2555浏览 • 1回复 待解决
HarmonyOS list嵌套MapComponent滑动冲突
588浏览 • 1回复 待解决
HarmonyOS scroll嵌套List不能整体滑动
1343浏览 • 1回复 待解决
HarmonyOS List嵌套waterflow滑动卡顿
1020浏览 • 1回复 待解决
HarmonyOS Scroll嵌套List滑动问题
785浏览 • 1回复 待解决
HarmonyOS Tab + List 性能优化
976浏览 • 1回复 待解决
Tab组件内嵌web view左右滑动切换问题
1094浏览 • 1回复 待解决
HarmonyOS scroll嵌套list页面无法滑动
1402浏览 • 1回复 待解决
HarmonyOS Scroll中嵌套List滑动事件冲突
816浏览 • 1回复 待解决