HarmonyOS 负反馈功能实现问题

搜索结果页采用瀑布流布局,现在需要实现负反馈功能。目前使用overlay不满足需求,因为overlay的前提是需要付组件有固定的height,搜索结果页的每个FlowItem都没有固定的height,所以无法使用。同时也试用了Stack组件也无法实现,是否有其他方案?

HarmonyOS
2024-12-25 15:00:58
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

需要在长按时获取当前FlowItem的高度,用这个高度作为浮层的高度,参考示例如下:

import { WaterFlowDataSource } from '../Modal/WaterFlowDataSource2';
import { componentUtils } from '@kit.ArkUI';

@Entry
@Component
struct WaterFlowDemo {
  minSize: number = 80
  maxSize: number = 180
  fontSize: number = 24
  colors: number[] = [0xFFC0CB, 0xDA70D6, 0x6B8E23, 0x6A5ACD, 0x00FFFF, 0x00FF7F]
  heights: string[] = ['30%', '35%', '40%', '45%', '50%', '55%']
  scroller: Scroller = new Scroller()
  dataSource: WaterFlowDataSource = new WaterFlowDataSource()
  @State overlayShow: boolean = false; // 浮层
  @State overlayHeight: number = 0;
  @State curIndex: number = -1;
  @State sections: WaterFlowSections = new WaterFlowSections()
  @State isShow: boolean = true;
  lastSection: SectionOptions = {
    itemsCount: 100, crossCount: 2
  }

  aboutToAppear() {
    let sectionOptions: SectionOptions[] = [];
    sectionOptions.push(this.lastSection);
    this.sections.splice(0, 0, sectionOptions);
  }

  @Builder
  OverlayNode(overlayHeight: number) {
    Column() {
      Button('关闭浮层')
        .type(ButtonType.Capsule)
        .fontSize(20)
        .margin({ bottom: 5 })
        .onClick(() => {
          this.overlayShow = false;
        })

      Text("This is overlayNode").fontSize(20).fontColor(Color.White)
    }
    .width('100%')
    .alignItems(HorizontalAlign.Center)
    .backgroundColor('#000000')
    .opacity(0.8)
    .height(overlayHeight)
  }

  build() {
    Column({ space: 2 }) {
      WaterFlow({ scroller: this.scroller }) {
        LazyForEach(this.dataSource, (item: number, index: number) => {
          FlowItem() {
            Stack({ alignContent: Alignment.Center }) {
              Text(index + "")
                .width("100%")
                .height(this.heights[index % 5])
            }
          }
          .id(item.toString())
          .width('100%')
          .overlay(this.curIndex === index && this.overlayShow ? this.OverlayNode(this.overlayHeight) : undefined,
            { align: Alignment.Center })
          // 单指长按文本触发该手势事件
          .gesture(
            LongPressGesture()// 由于repeat设置为true,长按动作存在时会连续触发,触发间隔为duration(默认值500ms)
              .onAction((event: GestureEvent) => {
                if (event) {
                  this.curIndex = index;
                  const componentInfo: componentUtils.ComponentInfo = componentUtils.getRectangleById(item.toString());
                  this.overlayHeight = px2vp(componentInfo.size.height); // 组件大小单位是px,需要转换下
                  this.overlayShow = true;
                  console.log('zkq FlowItem ' + item.toString() + ' ' + JSON.stringify(componentInfo.size));
                }
              })
          )
          .backgroundColor(this.colors[item % 5])
        }, (item: string) => item)
      }
      .columnsTemplate('1fr 1fr') // 瀑布流使用sections参数时该属性无效
      .columnsGap(10)
      .rowsGap(20)
      .backgroundColor(0xFAEEE0)
      .width("100%")
      .layoutWeight(1)
      .height("100%")
    }
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-25 16:43:16


相关问题
HarmonyOS RNOH问题反馈 - SVG组件
558浏览 • 1回复 待解决
HarmonyOS现在支持震动反馈吗?
860浏览 • 1回复 待解决
HarmonyOS Pixelmap相关反馈
596浏览 • 1回复 待解决
鸿蒙截图功能实现问题
10970浏览 • 1回复 待解决
HarmonyOS 推送功能问题
551浏览 • 1回复 待解决
HarmonyOS 录音功能问题
515浏览 • 1回复 待解决
HarmonyOS 如何实现轮询功能
633浏览 • 1回复 待解决
HarmonyOS AtomicInteger 计数功能实现
458浏览 • 1回复 待解决
HarmonyOS 分享功能如何实现
634浏览 • 1回复 待解决
HarmonyOS 如何实现ImagePreview功能
324浏览 • 1回复 待解决
HarmonyOS 换肤功能怎么实现
1134浏览 • 1回复 待解决
HarmonyOS 虚线功能实现
534浏览 • 1回复 待解决
HarmonyOS 实现RSA加密功能
828浏览 • 1回复 待解决
HarmonyOS 曝光功能如何实现
594浏览 • 1回复 待解决
HarmonyOS 如何实现popupwindow功能
434浏览 • 1回复 待解决
HarmonyOS 如何实现直播功能
633浏览 • 1回复 待解决
HarmonyOS 如何实现DeepLink功能
593浏览 • 1回复 待解决
HarmonyOS Nav和Router功能问题
454浏览 • 1回复 待解决
HarmonyOS 如何实现搜索历史功能
573浏览 • 1回复 待解决
HarmonyOS 如何实现图片编辑功能
633浏览 • 1回复 待解决