目录

滑动Swiper组件时如何不触发子组件的点击事件

问题现象

当前应用通过Swiper组件垂直滚动展示公告通知,点击子组件的内容会有弹窗提示,左右滑动时也会触发Text组件的点击事件,如何保证在左右滑动时不触发子组件的点击事件?

问题代码示例参考如下:

import { PromptAction } from '@kit.ArkUI';

@Entry
@Component
struct Index {
  promptAction: PromptAction = this.getUIContext().getPromptAction();

  build() {
    Column() {
      Swiper() {
        Text('公告')
          .width('80%')
          .fontSize(20)
          .textAlign(TextAlign.Center)
          .onClick(() => {
            this.promptAction.showToast({ message: '通知详情', alignment: Alignment.Top });
          })
      }
      .height('20%')
      .vertical(true)
    }
    .width('100%')
    .height('100%')
  }
}

问题效果预览:

背景知识

解决方案

可通过限制手势移动阈值区分点击、滑动事件。