HarmonyOS bindPopup遇到问题

给每个ListItem长按后增加了一个Popup

问题1:在外层设置了一个@state 的状态,长按后会同时弹出好几个Pop怎样监听按哪一个弹哪一个?

问题2:点击Pop后有删除当前条目的操作,怎么监听POP的点击?点击了删除后,怎么让Pop消失?

HarmonyOS
2024-12-18 16:31:53
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

可参考以下demo

import { router } from '@kit.ArkUI'
import { BusinessError } from '@kit.BasicServicesKit'

@Entry
@Component
export struct Index {
  @State list: stockListData = new stockListData([])

  aboutToAppear(): void {
    const list: ItemInfo[] = []
    for (let i = 0; i < 50; ++i) {
      let stockName = "item" + i
      let bgState = 0
      list.push(new ItemInfo(stockName, bgState))
    }
    this.list.modifyAllData(list)

  }

  build() {
    Column() {
      Text("开始").height('50')
        .width('100%').onClick(() => {
      })
      List({ space: 10 }) {
        LazyForEach(this.list, (info: ItemInfo, index: number) => {
          ListItem() {
            ItemView({
              info: info, itemFun: (name) => {
                let itemIndex = 0
                for (let im of this.list.getAllData()) {
                  if (im.stockName === name) {
                    break
                  }
                  itemIndex++
                }
                console.info("animate--ItemView--aboutToReuse---delete Name=" + name)
                this.list.getAllData().splice(itemIndex, 1)
                this.list.notifyDataDelete(itemIndex)
              }
            })
              .onClick(() => {
              })
          }.height('80')
        }, (info: ItemInfo, index: number) => info.stockName + "type" + index)
      }
      .height('100%')
      .width('100%')
    }
    .height('100%')
    .width('100%')
  }
}

@Reusable
@Component
struct ItemView {
  @Prop info: ItemInfo
  private lastName: string = ""

  aboutToAppear(): void {
    this.lastName = this.info.stockName
  }

  aboutToReuse(params: Record<string, Object>): void {
    console.info("animate--ItemView--aboutToReuse---name=" + this.info.stockName + ",lastName=" + this.lastName)

    this.lastName = this.info.stockName
  }

  @State customPopup: boolean = false
  itemFun: (name: string) => void = (name) => {
  }

  @Builder
  popItemsView() {
    Text("qipao pop").onClick(() => {
      this.customPopup = !this.customPopup
      this.itemFun(this.info.stockName)
    })
  }

  build() {
    Column() {
      Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
        Column() {
          Text(this.info.stockName)
            .maxFontSize('16')
            .minFontSize('8')
            .maxLines(1)
            .margin({ bottom: 2 })
        }
        .width('36.27%')
        .alignItems(HorizontalAlign.Start)
        .padding({ left: '16', right: 8 })
      }
      .backgroundColor("#00ffea")
      .height('100%')
    }
    .bindPopup(this.customPopup, {
      builder: this.popItemsView, // 气泡的内容
      placement: Placement.Top, // 气泡的弹出位置
      offset: { x: 0, y: 30 },
      radius: 12,
      popupColor: Color.Pink, // 气泡的背景色
      onStateChange: (e) => {
        console.info('tag', JSON.stringify(e.isVisible))
        if (!e.isVisible) {
          this.customPopup = false
          // this.bgColor = this.info.topStatus == 1 ? $r('app.color.dialog_background') : $r('app.color.start_window_background')
        } else {
          this.customPopup = true
          // this.bgColor = $r('app.color.dialog_button')
        }
      }
    })
    .gesture(
      LongPressGesture({ repeat: false })// 由于repeat设置为true,长按动作存在时会连续触发,触发间隔为duration(默认值500ms)
        .onAction((event?: GestureEvent) => {
          console.log("tag", "LongPressGesture start event--customPopup=" + this.customPopup)
          if (event) {
            this.customPopup = !this.customPopup
          }
          // if (event && event.repeat) {
          // }
        })// 长按动作一结束触发
        .onActionEnd((event?: GestureEvent) => {
          if (event) {
          }
        })
    )
  }
}

@Observed
class ItemInfo {
  @Track
  stockName: string = "--"
  @Track
  bgState: number = 0

  constructor(name: string, bg: number) {
    this.stockName = name
    this.bgState = bg
  }
}


class BasicDataSource implements IDataSource {
  private listeners: DataChangeListener[] = [];

  public totalCount(): number {
    return 0;
  }

  public getData(index: number): ItemInfo | undefined {
    return undefined;
  }

  registerDataChangeListener(listener: DataChangeListener): void {
    if (this.listeners.indexOf(listener) < 0) {
      this.listeners.push(listener);
    }
  }

  unregisterDataChangeListener(listener: DataChangeListener): void {
    const position = this.listeners.indexOf(listener);
    if (position >= 0) {
      this.listeners.splice(position, 1);
    }
  }

  notifyDataReload(): void {
    this.listeners.forEach((listener: DataChangeListener) => {
      listener.onDataReloaded();
    })
  }

  notifyDataAdd(index: number): void {
    this.listeners.forEach((listener: DataChangeListener) => {
      listener.onDataAdd(index);
    })
  }

  notifyDataChange(index: number): void {
    this.listeners.forEach((listener: DataChangeListener) => {
      listener.onDataChange(index);
    })
  }

  notifyDataDelete(index: number): void {
    this.listeners.forEach((listener: DataChangeListener) => {
      listener.onDataDelete(index);
    })
  }

  notifyDataMove(from: number, to: number): void {
    this.listeners.forEach((listener: DataChangeListener) => {
      listener.onDataMove(from, to);
    })
  }
}

export class stockListData extends BasicDataSource {
  private listData: ItemInfo[] = [];

  constructor(dataArray: ItemInfo[]) {
    super()
    this.listData = dataArray;
  }

  public totalCount(): number {
    return this.listData.length;
  }

  public getAllData(): ItemInfo[] {
    return this.listData;
  }

  public getData(index: number): ItemInfo {
    return this.listData[index];
  }

  public modifyAllData(data: ItemInfo[]): void {
    this.listData = data
    this.notifyDataReload()
  }
}
  • 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.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
分享
微博
QQ
微信
回复
2024-12-18 17:05:54
相关问题
HarmonyOS ArrayList 遇到问题
806浏览 • 1回复 待解决
HarmonyOS ArrayList 遇到问题
765浏览 • 0回复 待解决
求助:Ubuntu编译w800时遇到问题
6613浏览 • 1回复 待解决
HarmonyOS 组件.bindPopup属性使用问题
1067浏览 • 1回复 待解决
bindPopup样式问题有哪些啊?
932浏览 • 1回复 待解决
HarmonyOS bindPopup自定义气泡问题
518浏览 • 1回复 待解决
flutter混合遇到问题
1335浏览 • 1回复 待解决
HarmonyOS bindPopup设置color无效
1872浏览 • 0回复 待解决
应用导航设计遇到问题
1105浏览 • 1回复 待解决
HarmonyOS bindPopup的使用
840浏览 • 1回复 待解决
HarmonyOS bindPopup如何设置箭头颜色
913浏览 • 1回复 待解决
DevEco补全问题,有遇到的吗?
7033浏览 • 1回复 待解决
HarmonyOS bindPopup如何去掉圆角?
771浏览 • 1回复 待解决
鸿蒙js开发 video遇到问题
7429浏览 • 1回复 待解决
数据持久化遇到的各种问题
1131浏览 • 1回复 待解决