目录

Swiper组件被遮挡,导致点击事件不生效

问题现象

Swiper组件的点击事件不生效,应该如何处理?

问题代码示例参考如下:

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

@Entry
@ComponentV2
struct Index {
  @Local message: string = 'Hello World';
  swiperController: SwiperController = new SwiperController();
  imaglist: string[] = [
    'www.example.com',
    'www.example.com',
    'www.example.com',
    'www.example.com'
  ]
  promptAction: PromptAction = this.getUIContext().getPromptAction();
  build() {
    RelativeContainer() {
      Swiper(this.swiperController) {
        ForEach(this.imaglist, (imagurl: string, index: number) => {
          Image(imagurl)
            .width('100%')
            .height(300)
            .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP])
            .onClick(() => {
              this.promptAction.showToast({
                message: '第' + index + '张',
                duration: 1500,
                bottom: "center",
              })
            })
        })
      }
      .id("swiper")
      .clip(false)
      .autoPlay(true)
      .height(280)

      Column() {
        Text(this.message)
          .id('HelloWorld')
          .fontSize($r('app.float.page_text_font_size'))
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            this.message = 'Welcome';
            this.promptAction.showToast({
              message: '轮播图无法点击\n错误示范',
              duration: 1500,
              bottom: "center",
            })
          })
      }
      .width('100%')
      .height('100%')
      .justifyContent(FlexAlign.Center)
    }
    .height('100%')
    .width('100%')
  }
}

问题效果预览:

背景知识

问题定位

分析结论

Column组件遮挡Swiper组件,导致Swiper组件无法响应点击事件。

修改建议