鸿蒙HarmonyOS 5.0开发实战:多段混合数据展示实现案例

 往期鸿蒙5.0全套实战文章必看:(文中附带鸿蒙5.0全栈学习资料)


多段混合数据展示案例

介绍

本示例介绍了如何使用WaterFlow组件展示多列不同数据,通过设置sections属性来配置不同数据的列间距、行间距等信息。

效果图预览

使用说明

  1. 向下滑动,不同的数据可以规律展示。

实现思路

本示例通过设置WaterFlow组件的sections属性,来配置不同列/不同数据的相关信息,使得多段不同列数混合布局的数据能够正常展示。

  1. 初始化不同列输的分组信息。

    // 瀑布流是一列的时候,分组配置信息
    private oneColumnSection: SectionOptions = {
      // 分组中FlowItem数量
      itemsCount: 1,
      // 列数
      crossCount: 1,
      // 分组的列间距
      columnsGap: 5,
      // 分组的行间距
      rowsGap: 0,
      // 分组的margin
      margin: this.sectionMargin,
      // FlowItem的高度
      onGetItemMainSizeByIndex: (index: number) => {
        // 如果是最后一个item,高度赋值200
        if (index === this.dataCount - 1) {
          return 200;
        }
        return 160;
      }
    };
    // 瀑布流是两列的时候,分组配置信息
    private twoColumnSection: SectionOptions = {
      itemsCount: 8,
      crossCount: 2,
      columnsGap: 8,
      rowsGap: 0,
      onGetItemMainSizeByIndex: (index: number) => {
        // 瀑布流数据中最大的index是9的倍数,通过index除9的余数可以确定哪些item的高度较矮
        const newIndex = index % 9;
        // index除9的余数在以下数组中的的,高度较矮
        const longIndexArr = [1, 4, 5, 8];
        return longIndexArr.includes(newIndex) ? 155 : 256;
      }
    }
    
  2. 按照不同列数数据的出现顺序将分组信息存放到数组中。

    aboutToAppear() {
      let sectionOptions: SectionOptions[] = [];
      // 通过商品数据类型初始化瀑布流分组信息
      for (let index = 0; index < waterFlowData.length; index++) {
        const productInfo: ProductInfo = waterFlowData[index];
        if (productInfo.type === this.imageFlowItemReuseId) {
          // 仅展示图片时瀑布流是一列
          sectionOptions.push(this.oneColumnSection);
        } else if(productInfo.type === this.bottomFlowItem){
          // 瀑布流最后一个元素是一列
          sectionOptions.push(this.oneColumnSection);
        } else if (productInfo.type === this.reusableFlowItemReuseId) {
          // 图片文字混合时瀑布流是两列
          sectionOptions.push(this.twoColumnSection);
          index += (this.twoColumnSection.itemsCount - 1);
        }
      }
      this.sections.splice(0, 0, sectionOptions);
    }
    
  3. 使用WaterFlow组件构造页面,根据id构建不同的组件。

    WaterFlow({ scroller: this.scroller, sections: this.sections }) {
      LazyForEach(this.dataSource, (item: ProductInfo) => {
        FlowItem() {
          // 通过type字段区分需要展示的ux
          if (item.type === this.reusableFlowItemReuseId) {
            ReusableFlowItem({ listData: item })
              .reuseId(this.reusableFlowItemReuseId)
          } else if (item.type === this.imageFlowItemReuseId) {
            ReusableImageItem({ listData: item })
              .reuseId(this.imageFlowItemReuseId)
          } else {
            this.bottomItemInWaterFlow(item);
          }
        }
        .width($r('app.string.water_flow_item_width'))
      }, (item: ProductInfo) => JSON.stringify(item))
    }
    

高性能知识点

  1. 本示例构造子组件时,使用@Reusable标识组件,使其具有可复用的能力,减少性能消耗。

工程结构&模块类型

multicolumndisplay                                // har类型
|---/src/main/ets/components                        
|   |---MultiColumnDisplayPage.ets                // 多列不同数据展示案例首页
|   |---WaterFlowDataSource.ets                   // LazyForEach控制器
|---/src/main/ets/data                        
|   |---ProductData.ets                           // mock数据
|   |---ProductDataFormat.ets                     // 瀑布流数据结构

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值