HarmonyOS WaterFlow组件下的LazyForEach如何过滤无用的item?

this.dataSource是通用的数据源,瀑布流中的仅需dataSource中支持瀑布流的组件item,业务上需要过滤其他Item,但如果通过下述代码实现过滤,相当于没有设置FlowItem的子模块,但仍会存在空白区域(空白区域是因为columnsGap和rowsGap照成的),如何实现LazyForEach过滤无用的item且不留空白呢?

伪代码:

WaterFlow( ) {
  LazyForEach(this.dataSource, (item: NewsItem, index: number) => {
    FlowItem() {
      if(item.isWaterFlowType){
        NewsItemJiMeiNormalCardComponent({
          newsItem: newsItem,
          onNewsItemClick: this.onNewsItemClick,
          onNewsItemChildClick: this.onNewsItemChildClick
        })
      }
    }
  })
}
backgroundColor: Color.White,
columnsTemplate: '1fr 1fr',
rowsTemplate: '1fr',
columnsGap: 10,
rowsGap: 10,
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
HarmonyOS
2024-12-24 17:37:44
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

建议筛选并重新生成新的数据源给LazyForEach,以下是通过改样式达到效果,用了margin替代rowsGap:

@Entry
@Component
struct WaterFlowDemo {
  @State minSize: number = 80
  @State maxSize: number = 180
  @State colors: number[] = [0xFFC0CB, 0xDA70D6, 0x6B8E23, 0x6A5ACD, 0x00FFFF, 0x00FF7F]
  @State  dataSource: WaterFlowDataSource = new WaterFlowDataSource()
  private itemWidthArray: number[] = []
  private itemHeightArray: number[] = []

  // 计算FlowItem宽/高
  getSize() {
    let ret = Math.floor(Math.random() * this.maxSize)
    return (ret > this.minSize ? ret : this.minSize)
  }

  // 设置FlowItem宽/高数组
  setItemSizeArray() {
    for (let i = 0; i < 100; i++) {
      this.itemWidthArray.push(this.getSize())
      this.itemHeightArray.push(this.getSize())
    }
  }
  aboutToAppear() {
    this.setItemSizeArray()
  }

  build() {
    Column({ space: 2 }) {
      WaterFlow() {
        LazyForEach(this.dataSource, (item: number,index:number) => {
          if (index%4 === 1){
            FlowItem() {
              Column() {
                Text("N" + index).fontSize(12).height('16')
              }
            }
            .margin({bottom:5})
            .width('100%')
            .height(this.itemHeightArray[item % 100])
            .backgroundColor(this.colors[item % 5])
          }else{
            FlowItem()
          }
        }, (item: string) => item)
      }
      .columnsTemplate('repeat(auto-fill,80)')
      .columnsGap(10)
      .padding({left:5})
      // .rowsGap(5)
      .backgroundColor(0xFAEEE0)
      .width('100%')
      .height('100%')
    }
  }
}

// 实现IDataSource接口的对象,用于瀑布流组件加载数据
export class WaterFlowDataSource implements IDataSource {
  private dataArray: number[] = []
  constructor() {
    for (let i = 0; i < 100; i++) {
      this.dataArray.push(i)
    }
  }
  // 获取索引对应的数据
  public getData(index: number): number {
    return this.dataArray[index]
  }
  // 获取数据总数
  public totalCount(): number {
    return this.dataArray.length
  }
  // 注册改变数据的控制器
  registerDataChangeListener(listener: DataChangeListener): void {
  }
  // 注销改变数据的控制器
  unregisterDataChangeListener(listener: DataChangeListener): void {
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-24 19:17:03
相关问题
如何过滤textinput组件内容
1181浏览 • 1回复 待解决
HarmonyOS List组件WaterFlow组件增强
1594浏览 • 1回复 待解决
WaterFlow组件如何实现拖拽交换功能
281浏览 • 1回复 待解决
HarmonyOS listitem如何保存状态
815浏览 • 2回复 待解决