HarmonyOS Waterflow需要增加能力,支持某些元素强制展示在某一列上

首页置顶文章多屏适配要求,需要在多列下,强制展示在左侧。

HarmonyOS
2025-01-09 16:51:21
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

参考示例如下:

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

  public totalCount(): number {
    return 0;
  }

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

  // 该方法为框架侧调用,为LazyForEach组件向其数据源处添加listener监听
  registerDataChangeListener(listener: DataChangeListener): void {
    if (this.listeners.indexOf(listener) < 0) {
      console.info('add listener');
      this.listeners.push(listener);
    }
  }

  // 该方法为框架侧调用,为对应的LazyForEach组件在数据源处去除listener监听
  unregisterDataChangeListener(listener: DataChangeListener): void {
    const pos = this.listeners.indexOf(listener);
    if (pos >= 0) {
      console.info('remove listener');
      this.listeners.splice(pos, 1);
    }
  }

  // 通知LazyForEach组件需要重载所有子组件
  notifyDataReload(): void {
    this.listeners.forEach(listener => {
      listener.onDataReloaded();
    })
  }

  // 通知LazyForEach组件需要在index对应索引处添加子组件
  notifyDataAdd(index: number): void {
    this.listeners.forEach(listener => {
      listener.onDataAdd(index);
    })
  }

  // 通知LazyForEach组件在index对应索引处数据有变化,需要重建该子组件
  notifyDataChange(index: number): void {
    this.listeners.forEach(listener => {
      listener.onDataChange(index);
    })
  }

  // 通知LazyForEach组件需要在index对应索引处删除该子组件
  notifyDataDelete(index: number): void {
    this.listeners.forEach(listener => {
      listener.onDataDelete(index);
    })
  }

  // 通知LazyForEach组件将from索引和to索引处的子组件进行交换
  notifyDataMove(from: number, to: number): void {
    this.listeners.forEach(listener => {
      listener.onDataMove(from, to);
    })
  }
}

class MyDataSource extends BasicDataSource {
  private dataArray: string[] = [];
  private cursorOffset: number = 0;

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

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

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

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

  public getTopMostData(num: number) {
    this.cursorOffset = num;
    return this.dataArray.slice(0, num)
  }
}

@Entry
@Component
struct WaterFlowPage {
  private data: MyDataSource = new MyDataSource();

  aboutToAppear() {
    for (let i = 0; i <= 80; i++) {
      this.data.pushData(`Hello ${i}`)
    }
  }

  build() {
    WaterFlow() {
      FlowItem() {
        Column() {
          ForEach(this.data.getTopMostData(5), (item: string) => {
            Text(item).fontSize(20)
              .onAppear(() => {
                console.info("appear:" + item)
              })
          })
        }
      }.width("100%")
      //.height(200)
      .backgroundColor(Color.White)

      LazyForEach(this.data, (item: string, index: number) => {
        FlowItem() {
          Row() {
            Text(item).fontSize(20)
              .onAppear(() => {
                console.info("appear:" + item)
              })
          }
        }.width("100%")
        .height(30 + Math.random() * 30)
        .backgroundColor(Color.White)
      }, (item: string) => item)
    }
    .cachedCount(5)
    .columnsTemplate('1fr 1fr')
    .backgroundColor("#efefef")
    .columnsGap(10)
    .rowsGap(5)
  }
}
  • 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.
分享
微博
QQ
微信
回复
2025-01-09 19:42:45
相关问题
Waterflow怎么添加header能力?
1092浏览 • 1回复 待解决
HarmonyOS TLSSocket为什么强制需要bind
815浏览 • 1回复 待解决
HarmonyOS 需要“登录页面”通用能力
479浏览 • 1回复 待解决
判断某一年是不是闰年
1185浏览 • 1回复 待解决
HarmonyOS 是否支持系统类型增加协议
568浏览 • 1回复 待解决
HarmonyOS 如何只取消某一次的监听
830浏览 • 1回复 待解决