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

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

HarmonyOS
2024-05-26 12:32:47
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
savage01

使用的核心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) 
    } 
  } 
}

适配的版本信息

SDK:4.0.10.10IDE:

DevEco Studio 4.0.3.600

分享
微博
QQ
微信
回复
2024-05-27 16:16:53
相关问题
嵌套组件Scroll不生效
639浏览 • 1回复 待解决
Tabs组件嵌套滑动组件
588浏览 • 1回复 待解决
scroll和list嵌套滑动
399浏览 • 1回复 待解决
Scroll与WaterFlow滑动嵌套
408浏览 • 1回复 待解决
tabs结合scroll实现吸顶效果
339浏览 • 1回复 待解决
Web和List嵌套手势冲突问题
408浏览 • 1回复 待解决
怎么httpRequest添加cookie
218浏览 • 1回复 待解决
Tabs组件懒加载问题
747浏览 • 1回复 待解决
Scroll组件展示位置如何调整
708浏览 • 1回复 待解决
Scroll组件内显示不全问题
337浏览 • 1回复 待解决
自定义组件如何添加图片?
1114浏览 • 1回复 待解决
tabs组件 左右滑动延迟较高
441浏览 • 1回复 待解决
如何选择Navigation 组件Tabs 组件
698浏览 • 1回复 待解决
自定义组件嵌套组件
7854浏览 • 3回复 待解决