HarmonyOS NEXT星河版之实战商城App瀑布流(含加载更多)

一、目标

在这里插入图片描述

二、开撸

2.1 声明商品对象

export interface GoodsItem {
  title: string
  imageUrl: string
}

2.2 mock数据

export const mockGoodsList: GoodsItem[] = [
  {
    title: '宁雨昔美式复古字母三条杠圆领短袖T恤女纯棉宽松2024夏季新款学生上衣 白色 M',
    imageUrl: 'https://img10.360buyimg.com/n2/s240x240_jfs/t1/145307/22/41197/71267/65b5d932Fb67b2c27/cd986ae610999146.jpg!q70.jpg.webp'
  },
  {
    title: '京东京造【抗菌小白T】5A抑菌抗发黄T恤男新疆棉t恤男夏短袖打底T 白色L',
    imageUrl: 'https://img10.360buyimg.com/n2/s370x370_jfs/t1/230742/32/2823/51634/65520517F4c4f8ffb/6b8d81f6664085c6.jpg!q70.jpg.webp'
  },
  {
    title: 'YOOOURTHING男士休闲夹克青年时尚休闲连帽外套男薄款春秋季夹克男装 XZ1402-2130-卡其色 2XL【建议120-150斤】',
    imageUrl: 'https://img13.360buyimg.com/n2/s370x370_jfs/t1/100084/4/36774/78306/6423d6a8F913e76ad/80a8cdf35c09e19c.jpg!q70.jpg.webp'
  },
  {
    title: 'ZARA女装夏季新款宽松短袖T恤女 黑色 M',
    imageUrl: 'https://img10.360buyimg.com/n2/s370x370_jfs/t1/134131/23/44409/75625/661c013dF4a6fba04/3166211e54969778.jpg!q70.jpg.webp'
  },
  {
    title: '优衣库UT印花T恤女款 纯棉舒适圆领短袖上衣 白色 S',
    imageUrl: 'https://img14.360buyimg.com/n2/s370x370_jfs/t1/103976/1/31353/115998/630cc1cdE3a4eede9/bc1bc0bc560b775e.jpg!q70.jpg.webp'
  },
  {
    title: '小米手环5 智能运动手表 血氧检测心率监测 多功能NFC 时尚简约 黑色',
    imageUrl: 'https://img14.360buyimg.com/n2/s370x370_jfs/t1/100923/8/44254/52554/64f03948Fe911c342/59bea895f9ebdd38.jpg!q70.jpg.webp'
  },
  {
    title: '华为荣耀X10 5G手机 双模5G全网通 6GB+128GB 蓝水翡翠 移动联通电信5G 双卡双待手机',
    imageUrl: 'https://img0.baidu.com/it/u=2707221971,2703286647&fm=253&fmt=auto&app=138&f=JPEG?w=499&h=287'
  },
  {
    title: 'Apple MacBook Air 13.3英寸笔记本电脑 银色(Core i5 处理器/8GB内存/256GB固态硬盘)',
    imageUrl: 'https://img01.yzcdn.cn/upload_files/2020/05/21/FjX6bDV0pc4jSBKXgbDcDvtErMzR.jpg%21middle.jpg'
  },
  {
    title: '华为MateBook D 14 2020款 14英寸轻薄笔记本电脑(AMD Ryzen 5 3500U处理器/8GB内存/512GB固态硬盘)深空灰',
    imageUrl: 'https://img01.yzcdn.cn/upload_files/2017/10/19/FlYiuiKI5L17wHoALIx_txbCeOME.jpg%21middle.jpg'
  },
  {
    title: '联想(Lenovo)小新Pro13高性能轻薄本笔记本电脑 13.3英寸全面屏学生办公商务便携轻薄本(标压R5-3550H 16G 512G FHD IPS)银',
    imageUrl: 'https://img2.baidu.com/it/u=1282939547,1976310423&fm=253&fmt=auto&app=138&f=JPEG?w=375&h=500'
  }
]

2.3 主页面

import { GoodsItem, mockGoodsList } from './modules';

@Entry
@Component
struct WaterFlowGoodsPage {
  @State goodsList: GoodsItem[] = mockGoodsList

  build() {
    WaterFlow() {
      ForEach(this.goodsList, (item: GoodsItem, index: number) => {
        FlowItem() {
          Column({ space: 5 }) {
            Image(item.imageUrl)
              .height(index % 2 ? 120 : 180)
              .borderRadius(8)
            Text(item.title)
              .fontSize(14)
              .lineHeight(22)
              .maxLines(3)
              .textOverflow({ overflow: TextOverflow.Ellipsis })
          }
        }
      })
    }
    .height('100%')
    .columnsTemplate('1fr 1fr')
    .columnsGap(10)
    .rowsGap(10)
    .padding(10)
  }
}

2.4 加载更多

在滑动到底部时,加载更多数据,如下:
在这里插入图片描述
WaterFlow参数中有footer,代表了底部内容。增加getFooter方法,如下:

@Builder
getFooter() {
  Row() {
    Text('加载更多...')
  }
  .justifyContent(FlexAlign.Center)
  .backgroundColor(Color.Pink)
  .height(60)
  .width('100%')
  }

WaterFlow设置footer

WaterFlow({ footer: this.getFooter }) {}

设置变量isLoadMore判断是否正在加载中:

@State isLoadMore: boolean = false

设置WaterFlow的滑动到底部事件:

// 滑动到底部
.onReachEnd(async () => {
  if (!this.isLoadMore) {
    try {
      this.isLoadMore = true
      await this.loadMore()
      this.isLoadMore = false
    } catch (error) {
      promptAction.showToast({ message: JSON.stringify(error) })
    }
  }
})

加载更多数据方法:

/**
 * 加载更多
 * @returns
 */
loadMore() {
  return new Promise<boolean>((resolve, reject) => {
    // mock网络请求
    setTimeout(() => {
      this.goodsList.push(...this.goodsList.slice(0, 5))
    }, 2000)
    resolve(true)
  })
}

2.5 完整代码

import { GoodsItem, mockGoodsList } from './modules';
import { promptAction } from '@kit.ArkUI';

@Entry
@Component
struct WaterFlowGoodsPage {
  @State goodsList: GoodsItem[] = mockGoodsList
  @State isLoadMore: boolean = false

  @Builder
  getGoodsItemView(item: GoodsItem, index: number) {
    Column({ space: 5 }) {
      Image(item.imageUrl)
        .height(index % 2 ? 120 : 180)
        .borderRadius(8)
      Text(item.title)
        .fontSize(14)
        .lineHeight(22)
        .maxLines(3)
        .textOverflow({ overflow: TextOverflow.Ellipsis })
    }
  }

  @Builder
  getFooter() {
    Row() {
      Text('加载更多...')
    }
    .justifyContent(FlexAlign.Center)
    .backgroundColor(Color.Pink)
    .height(60)
    .width('100%')
  }

  build() {
    WaterFlow({ footer: this.getFooter }) {
      ForEach(this.goodsList, (item: GoodsItem, index: number) => {
        FlowItem() {
          this.getGoodsItemView(item, index)
        }
      })
    }
    .height('100%')
    .columnsTemplate('1fr 1fr')
    .columnsGap(10)
    .rowsGap(10)
    .padding(10)
    // 滑动到底部
    .onReachEnd(async () => {
      if (!this.isLoadMore) {
        try {
          this.isLoadMore = true
          await this.loadMore()
          this.isLoadMore = false
        } catch (error) {
          promptAction.showToast({ message: JSON.stringify(error) })
        }
      }
    })
  }

  /**
   * 加载更多
   * @returns
   */
  loadMore() {
    return new Promise<boolean>((resolve, reject) => {
      // mock网络请求
      setTimeout(() => {
        this.goodsList.push(...this.goodsList.slice(0, 5))
      }, 2000)
      resolve(true)
    })
  }
}

三、小结

  • WaterFlow组件使用
  • 监听滑动底部事件onReachEnd
  • 加载更多处理
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Swiper加载更多的功能,你可以使用Swiper的事件和方法来实现。以下是一个简单的示例: 首先,在HTML中创建一个Swiper容器: ```html <div class="swiper-container"> <div class="swiper-wrapper"> <!-- 这里放置你的Swiper滑动项 --> </div> <div class="swiper-pagination"></div> <div class="swiper-button-next"></div> <div class="swiper-button-prev"></div> </div> ``` 然后,在JavaScript中初始化Swiper实例并添加加载更多的功能: ```javascript var mySwiper = new Swiper('.swiper-container', { // 初始化Swiper的配置项 // ... on: { reachEnd: function() { // 当滑动到最后一个滑动项时触发 // 在这里执行加载更多的操作 // 示例:向Swiper中添加新的滑动项 this.appendSlide('<div class="swiper-slide">New Slide</div>'); } } }); // 示例:点击按钮触发加载更多 document.getElementById('loadMoreBtn').addEventListener('click', function() { // 在这里执行加载更多的操作 // 示例:向Swiper中添加新的滑动项 mySwiper.appendSlide('<div class="swiper-slide">New Slide</div>'); }); ``` 在上述示例中,`reachEnd`事件会在滑动到最后一个滑动项时触发,你可以在该事件回调函数中执行加载更多的操作,比如向Swiper中添加新的滑动项。另外,你还可以通过其他的方式触发加载更多的操作,比如点击按钮。 请注意,这只是一个简单的示例,你可以根据自己的需求进行修改和扩展。具体的Swiper配置和方法可以参考Swiper的官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值