HarmonyOS waterFlow无法像GridItem设置item占一行

想要将某个item设置占一行跟着waterFlow一起滑动。

HarmonyOS
2024-10-15 10:21:22
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

参考demo:

import {WaterFlowDataSource} from './WaterFlowDataSource'  
@Entry  
@Component  
struct WaterFlowDemo {  
  @State minSize: number = 80  
  @State maxSize: number = 180  
  @State fontSize: number = 24  
  @State colors: number[] = [0xFFC0CB, 0xDA70D6, 0x6B8E23, 0x6A5ACD, 0x00FFFF, 0x00FF7F]  
  scroller: Scroller = new Scroller()  
  datasource: WaterFlowDataSource = new WaterFlowDataSource()  
  private itemWidthArray: number[] = []  
  private itemHeightArray: number[] = []  
  // 计算flow item宽/高  
  getSize() {  
    let ret = Math.floor(Math.random() * this.maxSize)  
    return (ret > this.minSize ? ret : this.minSize)  
  }  
// 保存flow item宽/高  
getItemSizeArray() {  
  for (let i = 0; i < 1000; i++) {  
    this.itemWidthArray.push(this.getSize())  
    this.itemHeightArray.push(this.getSize())  
  }  
}  
aboutToAppear() {  
  this.getItemSizeArray()  
}  
@Builder  
itemFoot() {  
    Column() {  
      Text(`Footer`)  
        .fontSize(10)  
        .backgroundColor(Color.Red)  
        .width(50)  
        .height(50)  
        .align(Alignment.Center)  
        .margin({ top: 2 })  
    }  
}  
build() {  
    Column({ space: 2 }) {  
      WaterFlow() {  
        LazyForEach(this.datasource, (item: number, index: number) => {  
          FlowItem() {  
            Column() {  
              if(this.itemWidthArray[index] > 100){  
                Grid() {  
                  GridItem() {  
                    Text("N" + item)  
                      .fontSize(16)  
                      .width('100%')  
                      .height('100%')  
                      .textAlign(TextAlign.Center)  
                  }.columnStart(1).columnEnd(2)  
                }  
              }else{  
                Grid() {  
                  GridItem() {  
                    Text("N" + item)  
                      .fontSize(16)  
                      .width('100%')  
                      .height('100%')  
                      .textAlign(TextAlign.Center)  
                      .borderStyle(BorderStyle.Dashed).borderWidth(5).borderColor(Color.Red).borderRadius(10)  
                  }  
                  GridItem() {  
                    Text("N1" + item)  
                      .fontSize(16)  
                      .width('100%')  
                      .height('100%')  
                      .textAlign(TextAlign.Center)  
                      .borderStyle(BorderStyle.Dashed).borderWidth(5).borderColor(Color.Green).borderRadius(10)  
                  }  
                }  
                .columnsTemplate('1fr 1fr')  
              }  
            }  
          }  
          .onAppear(() => {  
            // 即将触底时提前增加数据  
            if (item + 20 == this.datasource.totalCount()) {  
              for (let i = 0; i < 100; i++) {  
                this.datasource.AddLastItem()  
              }  
            }  
          })  
          .width('100%')  
          .height(this.itemHeightArray[item % 100])  
          .backgroundColor(this.colors[item % 5])  
        }, (item: string) => item)  
      }  
.columnsTemplate("1fr")  
  .columnsGap(10)  
  .rowsGap(5)  
  .backgroundColor(0xFAEEE0)  
  .width('100%')  
  .height('100%')  
}  
}  
}
export class WaterFlowDataSource implements IDataSource {  
  private dataArray: number[] = []  
  private listeners: DataChangeListener[] = []  
  constructor() {  
    for (let i = 0; i < 1000; i++) {  
      this.dataArray.push(i)  
    }  
  }  
  // 获取索引对应的数据  
  public getData(index: number): number {  
    return this.dataArray[index]  
  }  
  // 通知控制器数据重新加载  
  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)  
    })  
  }  
  // 获取数据总数  
  public totalCount(): number {  
    return this.dataArray.length  
  }  
  // 注册改变数据的控制器  
  registerDataChangeListener(listener: DataChangeListener): void {  
    if (this.listeners.indexOf(listener) < 0) {  
      this.listeners.push(listener)  
    }  
  }  
  // 注销改变数据的控制器  
  unregisterDataChangeListener(listener: DataChangeListener): void {  
    const pos = this.listeners.indexOf(listener)  
    if (pos >= 0) {  
      this.listeners.splice(pos, 1)  
    }  
  }  
  // 增加数据  
  public Add1stItem(): void {  
    this.dataArray.splice(0, 0, this.dataArray.length)  
    this.notifyDataAdd(0)  
  }  
  // 在数据尾部增加一个元素  
  public AddLastItem(): void {  
    this.dataArray.splice(this.dataArray.length, 0, this.dataArray.length)  
    this.notifyDataAdd(this.dataArray.length - 1)  
  }  
  // 在指定索引位置增加一个元素  
  public AddItem(index: number): void {  
    this.dataArray.splice(index, 0, this.dataArray.length)  
    this.notifyDataAdd(index)  
  }  
  // 删除第一个元素  
  public Delete1stItem(): void {  
    this.dataArray.splice(0, 1)  
    this.notifyDataDelete(0)  
  }  
  // 删除第二个元素  
  public Delete2ndItem(): void {  
    this.dataArray.splice(1, 1)  
    this.notifyDataDelete(1)  
  }  
  // 删除最后一个元素  
  public DeleteLastItem(): void {  
    this.dataArray.splice(-1, 1)  
    this.notifyDataDelete(this.dataArray.length)  
  }  
  // 重新加载数据  
  public Reload(): void {  
    this.dataArray.splice(1, 1)  
    this.dataArray.splice(3, 2)  
    this.notifyDataReload()  
  }  
}
分享
微博
QQ
微信
回复
2024-10-15 17:40:57
相关问题
HarmonyOS 一行文案不同字体大小
53浏览 • 1回复 待解决
JS swiper 怎么list样动态添加item
6356浏览 • 1回复 待解决
HarmonyOS 字体高应该如何设置
53浏览 • 1回复 待解决
HarmonyOS TextArea如何设置单行的高?
286浏览 • 1回复 待解决
HarmonyOS GridItem自适应高度问题
416浏览 • 1回复 待解决