在Scroll里面嵌套的tabs组件中添加手势操作

手势事件无法在Scroll组件中实行上滑下滑操作,比如需要下拉刷新页面。

HarmonyOS
2024-05-26 12:32:47
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
薯条包装盒

使用的核心API

使用onTouch事件来替代parallelGesture事件

核心代码解释

onTouch事件不会参与到手势竞争

 .onTouch((event) => { 
    console.log(event.touches[0].y + "") 
    if (event.touches[0].type == TouchType.Down) { 
    this.downTouchY = event.touches[0].y 
    this.lastTouchY = event.touches[0].y 
    this.isScroll = false; 
  }
@Entry 
@Component 
struct RefreshListPage { 
  @State message: string = 'Hello World' 
  @State arr: number[] = [] 
  @State hitTest: HitTestMode = HitTestMode.None; 
  
  isScroll: boolean = false; 
  downTouchY: number = -1 
  lastTouchY: number = -1; 
  
  @Styles 
  listCard() { 
    .backgroundColor(Color.White) 
    .height(200) 
    .width("100%") 
    .borderRadius(12) 
  } 
  
  build() { 
    Scroll() { 
      Column() { 
        Text("Scroll Area") 
          .width("100%") 
          .height("30%") 
          .backgroundColor('#0080DC') 
          .textAlign(TextAlign.Center) 
        Tabs({ barPosition: BarPosition.Start }) { 
          TabContent() { 
            Column() { 
              List({ space: 10 }) { 
                ForEach(this.arr, (item: number) => { 
                  ListItem() { 
                    Text("item" + item) 
                      .fontSize(16) 
                  }.listCard() 
                }, (item: string) => item) 
              } 
              .width("100%") 
              .height('100%') 
              .edgeEffect(EdgeEffect.None) 
              .nestedScroll({ 
                scrollForward: NestedScrollMode.PARENT_FIRST, 
                scrollBackward: NestedScrollMode.SELF_FIRST 
              }) 
              .onTouch((event) => { 
                console.log(event.touches[0].y + "") 
                if (event.touches[0].type == TouchType.Down) { 
                  this.downTouchY = event.touches[0].y 
                  this.lastTouchY = event.touches[0].y 
                  this.isScroll = false; 
                } 
                if (event.touches[0].type == TouchType.Move) { 
                  //滑动的最小距离为5vp时拖动手势识别成功。 
                  if (Math.abs(event.touches[0].y - this.lastTouchY) > 5) { 
                    if (!this.isScroll) { 
                      this.isScroll = true; 
                      console.info("[Column] onActionStart") 
                    } else { 
                      console.info("[Column] onActionUpdate") 
                    } 
                  } 
                  this.lastTouchY = event.touches[0].y 
                } 
                if (event.touches[0].type == TouchType.Up) { 
                  console.info("[Column] onActionEnd") 
                } 
              }) 
            } 
            .width('100%').height('100%') 
            // .parallelGesture( 
            //   PanGesture(new PanGestureOptions({ direction: PanDirection.Up | PanDirection.Down })) 
            //     .onActionStart((event: GestureEvent) => { 
            //       console.info("[Column] onActionStart") 
            //     }) 
            //     .onActionUpdate((event: GestureEvent) => { 
            //       console.info("[Column] onActionUpdate") 
            //     }) 
            //     .onActionEnd(() => { 
            //       console.info("[Column] onActionEnd") 
            //     }) 
            // ) 
          }.tabBar("Tab1") 
  
          TabContent() { 
          }.tabBar("Tab2") 
        } 
        .vertical(false) 
        .height("100%") 
      }.width("100%") 
    } 
    .edgeEffect(EdgeEffect.None) 
    .friction(0.6) 
    .backgroundColor('#DCDCDC') 
    .scrollBar(BarState.Off) 
    .width('100%') 
    .height('100%') 
    .onTouch((event) => { 
      console.log('[Scroll]', event.type) 
    }) 
  } 
  
  aboutToAppear() { 
    this.hitTest = HitTestMode.Default 
    for (let i = 0; i < 3; i++) { 
      this.arr.push(i) 
    } 
  } 
}
  • 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.

适配的版本信息

SDK:4.0.10.10IDE:

DevEco Studio 4.0.3.600

分享
微博
QQ
微信
回复
2024-05-27 16:16:53
相关问题
嵌套组件Scroll不生效
2816浏览 • 1回复 待解决
HarmonyOS Scroll嵌套web手势冲突
685浏览 • 1回复 待解决
HarmonyOS Tabs组件嵌套Tabs组件问题
1769浏览 • 1回复 待解决
Tabs组件嵌套滑动组件
2425浏览 • 1回复 待解决
如何在TabstabBar,添加其他组件
1519浏览 • 1回复 待解决
HarmonyOS Tabs组件嵌套滑动
1168浏览 • 1回复 待解决
HarmonyOS Scroll嵌套List滑动事件冲突
824浏览 • 1回复 待解决
scroll和list嵌套滑动
2570浏览 • 1回复 待解决
HarmonyOS Tabs组件怎么动态添加TabContent
1049浏览 • 1回复 待解决
HarmonyOS Scroll 嵌套 RelativeContainer 问题
1249浏览 • 1回复 待解决