HarmonyOS lazyForEach相关范例

lazyForEach相关范例

HarmonyOS
2024-12-20 17:52:16
783浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

lazyForEach原理介绍 https://developer.huawei.com/consumer/cn/doc/best-practices-V5/bpta-best-practices-long-list-V5

demo示例:

interface listItem {
  money: string
  text: string
  title: string
  data: string
}

class StringData {
  money: string
  text: string
  title: string
  data: string
  constructor(money: string,text: string, title: string, data: string) {
    this.money = money;
    this.text = text;
    this.title = title;
    this.data = data;
  }
}

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

  public totalCount(): number {
    return 0;
  }

  public getData(index: number): StringData {
    return this.originDataArray[index];
  }

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

  unregisterDataChangeListener(listener: DataChangeListener): void {
    const pos = this.listeners.indexOf(listener);
    if (pos >= 0) {
      console.info('remove listener');
      this.listeners.splice(pos, 1);
    }
  }

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

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

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

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

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

class MyDataSource extends BasicDataSource {
  private dataArray: StringData[] = [];

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

  public getData(index: number): StringData {
    return this.dataArray[index];
  }

  public addData(index: number, data: StringData): void {
    this.dataArray.splice(index, 0, data);
    this.notifyDataAdd(index);
  }

  public pushData(data: StringData): void {
    this.dataArray.push(data);
    this.notifyDataAdd(this.dataArray.length - 1);
  }

  public reloadData(): void {
    this.notifyDataReload();
  }
}

@Entry
@Component
struct Index {
  private title: string = '我的优惠券'
  private data: MyDataSource = new MyDataSource();
  @State listDate: listItem[] = [
    {
      money: '¥360',
      text: '满3500元立减',
      title: '限部分商品使用',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥250',
      text: '满2500元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥200',
      text: '满1800元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥150',
      text: '满1300元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥80',
      text: '满800元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥360',
      text: '满3500元立减',
      title: '限部分商品使用',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥250',
      text: '满2500元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥200',
      text: '满1800元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥150',
      text: '满1300元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥80',
      text: '满800元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥360',
      text: '满3500元立减',
      title: '限部分商品使用',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥250',
      text: '满2500元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥200',
      text: '满1800元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥150',
      text: '满1300元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥80',
      text: '满800元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥360',
      text: '满3500元立减',
      title: '限部分商品使用',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥250',
      text: '满2500元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥200',
      text: '满1800元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥150',
      text: '满1300元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥80',
      text: '满800元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥360',
      text: '满3500元立减',
      title: '限部分商品使用',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥250',
      text: '满2500元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥200',
      text: '满1800元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥150',
      text: '满1300元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥80',
      text: '满800元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥360',
      text: '满3500元立减',
      title: '限部分商品使用',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥250',
      text: '满2500元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥200',
      text: '满1800元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥150',
      text: '满1300元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    },
    {
      money: '¥80',
      text: '满800元立减',
      title: '适用于体验',
      data: '2024.02.23-2024.03.15'
    }
  ]

  aboutToAppear(): void {
    // 请求数据模拟
    for (let i = 0; i <= this.listDate.length; i++) {
      this.data.pushData(new StringData(this.listDate[i].money,this.listDate[i].text,this.listDate[i].title,this.listDate[i].data));
    }
    console.log(JSON.stringify(this.data),'[Log]')
  }

  build() {
    Column() {
      Text(this.title)
        .fontSize(18)
        .fontWeight(FontWeight.Bold)
        .fontFamily('Arial')
      Row() {
        TextInput({ placeholder: '请输入优惠码' })
          .width('75%')
          .fontColor(Color.Blue)
          .fontSize(14)
          .fontStyle(FontStyle.Italic)
          .fontWeight(FontWeight.Bold)
          .fontFamily('Arial')
          .placeholderColor(0x999999)
          .placeholderFont({ size: 16, weight: FontWeight.Medium, family: 'cursive', style: FontStyle.Italic })
        Button('添加',{ type: ButtonType.Normal, stateEffect: true })
          .width('25%')
          .fontSize(14)
          .fontWeight(FontWeight.Medium)
          .backgroundColor('#cccccc')
          .borderRadius(5)
      }
      .padding({ top: 35, bottom: 10})

      Row() {
        Text(` 有${this.data.totalCount()}张优惠券可领用~`)
          .fontSize(12)
          .textAlign(TextAlign.End)
        Text('>')
      }
      .margin({bottom: 10})

      List({ space: 10 }) {
        LazyForEach(this.data, (item: listItem) => {
          ListItem() {
            Row() {
              Column() {
                Text(item.money)
                  .margin({top:20, bottom: 20})
                  .fontColor('#f9f4d7')
                  .borderRadius(5)
                Text(item.text)
                  .textAlign(TextAlign.Center)
                  .fontSize(12)
                  .fontColor('#f9f4d7')
                  .borderRadius(5)
              }
              .width('30%')
              .height(100)
              .backgroundColor('#fe6a48')
              Column() {
                Row() {
                  Text(item.title)
                    .textAlign(TextAlign.Start)
                    .margin({top: 15, left: -110, bottom: 40})
                }
                Row() {
                  Text(item.data)
                    .fontSize(10)
                    .margin({left: -5, right: 25})
                  Button('去使用', { type: ButtonType.Capsule })
                    .width('32%')
                    .height(20)
                    .fontSize(12)
                    .fontColor('#f6a200')
                    .backgroundColor('#cccccc')
                    .borderColor('#f6a200')
                }
              }
              .width('70%')
              .height(100)
              .backgroundColor('#fff')
            }
          }
        })
      }
    }
    .padding(12)
    .height('100%')
    .backgroundColor(0xF1F3F5)
  }
}
  • 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.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.
  • 244.
  • 245.
  • 246.
  • 247.
  • 248.
  • 249.
  • 250.
  • 251.
  • 252.
  • 253.
  • 254.
  • 255.
  • 256.
  • 257.
  • 258.
  • 259.
  • 260.
  • 261.
  • 262.
  • 263.
  • 264.
  • 265.
  • 266.
  • 267.
  • 268.
  • 269.
  • 270.
  • 271.
  • 272.
  • 273.
  • 274.
  • 275.
  • 276.
  • 277.
  • 278.
  • 279.
  • 280.
  • 281.
  • 282.
  • 283.
  • 284.
  • 285.
  • 286.
  • 287.
  • 288.
  • 289.
  • 290.
  • 291.
  • 292.
  • 293.
  • 294.
  • 295.
  • 296.
  • 297.
  • 298.
  • 299.
  • 300.
  • 301.
  • 302.
  • 303.
  • 304.
  • 305.
  • 306.
  • 307.
  • 308.
  • 309.
  • 310.
  • 311.
  • 312.
  • 313.
  • 314.
  • 315.
  • 316.
  • 317.
  • 318.
  • 319.
  • 320.
  • 321.
  • 322.
  • 323.
  • 324.
  • 325.
  • 326.
  • 327.
  • 328.
  • 329.
  • 330.
  • 331.
  • 332.
  • 333.
  • 334.
  • 335.
  • 336.
  • 337.
  • 338.
  • 339.
  • 340.
  • 341.
  • 342.
  • 343.
  • 344.
  • 345.
  • 346.
  • 347.
  • 348.
  • 349.
  • 350.
  • 351.
  • 352.
  • 353.
  • 354.
  • 355.
  • 356.
  • 357.
  • 358.
  • 359.
  • 360.
  • 361.
  • 362.
  • 363.
  • 364.
  • 365.
  • 366.
  • 367.
  • 368.
  • 369.
  • 370.
  • 371.
  • 372.
  • 373.
  • 374.
  • 375.
  • 376.
  • 377.
  • 378.
  • 379.
  • 380.
  • 381.
  • 382.
  • 383.
分享
微博
QQ
微信
回复
2024-12-20 19:03:56


相关问题
使用LazyForEach懒加载列表相关问题
1719浏览 • 1回复 待解决
WaterFlow的使用范例有哪些?
1836浏览 • 1回复 待解决
HarmonyOS LazyForEach
825浏览 • 1回复 待解决
HarmonyOS lazyforEach渲染问题
690浏览 • 1回复 待解决
HarmonyOS lazyForEach的key问题
443浏览 • 1回复 待解决
HarmonyOS lazyForeach嵌套视图问题
884浏览 • 1回复 待解决
HarmonyOS 如何正确使用LazyForEach
609浏览 • 1回复 待解决
HarmonyOS lazyForEach数据应用问题
679浏览 • 1回复 待解决
HarmonyOS lazyforeach报错,见截图
726浏览 • 1回复 待解决
HarmonyOS swiper + LazyForEach使用问题
1132浏览 • 1回复 待解决
HarmonyOS LazyForEach数据刷新问题
650浏览 • 1回复 待解决
HarmonyOS LazyForEach 不会懒加载原因
729浏览 • 1回复 待解决
HarmonyOS LazyForEach组件dataSource使用问题
1505浏览 • 2回复 待解决
HarmonyOS LazyForEach问题刷新UI问题
934浏览 • 1回复 待解决
list 如何使用 lazyforeach
940浏览 • 1回复 待解决
ListItemGroup 和Lazyforeach结合场景
1670浏览 • 1回复 待解决
HarmonyOS 关于Swiper+LazyForEach方案咨询
767浏览 • 1回复 待解决
LazyForEach如何添加数据?
2612浏览 • 1回复 待解决
HarmonyOS LazyForEach不会更新@State里的值
786浏览 • 1回复 待解决