有什么办法能根据不同的手势类型(点击、拖动)来决定是否用哪个子组件来响应不?

鸿蒙开发中的自定义事件分发中,TouchTestInfo并没有触摸的手势类型,有什么办法能根据不同的手势类型(点击、拖动)来决定是否用哪个子组件来响应不?
场景描述:有个View全屏覆盖在List上,希望可以点击的时候,View正常响应。滑动的时候,下方的List可以正常滚动不受影响,并且只处理上下滑手势,左右不响应。

-- 来自灵芸小骏老师直播间

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

阅读 592
1 个回答

具体解决方案:

import promptAction from '@ohos.promptAction'; 
 
@Entry 
@Component 
struct ListExample { 
  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] 
  @State text: string = 'Button' 
 
  build() { 
    Stack() { 
      List({ space: 12, initialIndex: 0 }) { 
        ForEach(this.arr, (item: number) => { 
          ListItem() { 
            Text('Item ' + item) 
              .width('100%') 
              .height(56) 
              .fontSize(16) 
              .textAlign(TextAlign.Start) 
          }.borderRadius(24) 
          .backgroundColor(Color.White) 
          .padding({ left: 12, right: 12 }) 
        }, (item: string) => item) 
      } 
      .listDirection(Axis.Vertical) 
      .scrollBar(BarState.Off) 
      .edgeEffect(EdgeEffect.Spring) 
      .onScrollIndex((start: number, end: number) => { 
        console.info('first' + start) 
        console.info('last' + end) 
      }) 
      .onScroll((scrollOffset: number, scrollState: ScrollState) => { 
        console.info(`onScroll scrollState = ScrollState` + scrollState + `, scrollOffset = ` + scrollOffset) 
      }) 
      .width('100%') 
      .height('100%') 
      .id('MyList') 
      Column() 
        .width('100%') 
        .height('100%') 
        .backgroundColor('#FA8072') 
        .onClick(()=>{ 
          console.log(' click') 
        }) 
    } 
    .width('100%') 
    .height('100%') 
    .backgroundColor(0xF1F3F5) 
    .padding({ left: 12, right: 12, bottom: 24 }) 
    .onChildTouchTest((touchinfo) => { 
      for (let info of touchinfo) { 
        if (info.id == 'MyList') { 
          return { id: info.id, strategy: TouchTestStrategy.FORWARD_COMPETITION } 
        } 
      } 
      return { strategy: TouchTestStrategy.DEFAULT } 
    }) 
  } 
}

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进