鸿蒙开发:案例集合List:模拟附近人列表插入

🎯 案例集合List:模拟附近人列表插入

🌍 案例集合List

🚪 最近开启了学员班级点我欢迎加入

🏷️ 效果图(录制有点掉帧)

📖 参考

🧩 拆解

  • 从列表前面插入(后续会增加高性能的方式)
javascript 复制代码
@Component
export struct ListItemUnshiftAnimation {
  /**
   * 模拟数据
   */
  @State mockData: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  /**
   * 监听是否滑动到List顶部
   */
  @State @Watch('isListTopChange') isListTop: boolean = false
  /**
   * 定时器ID
   */
  @State timerId: number = -1

  /**
   * 滑动到头开启延时器追加数据
   */
  isListTopChange() {
    if (this.isListTop) {
      this.timerId = setInterval(() => {
        this.getUIContext().animateTo({ duration: 300, curve: Curve.Smooth }, () => {
          this.mockData.unshift(this.mockData.length + 1)
        })
      }, 2000)
    }
  }

  /**
   * 模拟Swipe组件或者banner组件
   */
  @Builder
  mockSwipeBuilder() {
    Stack({ alignContent: Alignment.Center }) {
      Image($r('app.media.startIcon'))
        .width('100%')
        .height(200)
        .draggable(false)
        .borderRadius(10)
      Text('模拟Swipe组件')
        .fontSize(20)
        .fontWeight(FontWeight.Bold)
    }
    .height(200)
    .width('100%')
  }

  aboutToDisappear(): void {
    this.isListTop = false // 关闭追加数据
    clearInterval(this.timerId) // 清空延时器
  }

  build() {
    Column() {
      this.mockSwipeBuilder()

      List({ space: 20 }) {
        ListItem() // 这里处理是否滑动到顶部占位组件
        ForEach(this.mockData, (item: number) => {
          ListItem() {
            Text(item.toString())
              .height(150)
              .width('100%')
              .borderRadius(10)
              .backgroundColor(Color.Orange)
              .textAlign(TextAlign.Center)
          }
          // 170 = 150的组件高度 20的组件边距,这样设置不会有挤压 | 割裂的效果
          .transition(TransitionEffect.OPACITY.combine(TransitionEffect.translate({ y: -170 })))

          // 这里在实际业务中需要取字段中的唯一做标识,不然会有很奇怪的问题
        }, (item: number) => item.toString())
      }
      .cachedCount(1, true) // 预加载数量1,true代表需要显示
      .width('100%')
      .layoutWeight(1)
      .scrollBar(BarState.Off) // 关闭滚动条
      .onReachStart(() => this.isListTop = true) // 是否滑动到头
      .onScrollIndex(() => {
        this.isListTop = false // 关闭追加数据
        clearInterval(this.timerId) // 清空延时器
      })
    }
    .height('100%')
    .width('100%')
  }
}

🌸🌼🌺

相关推荐
GitCode官方1 小时前
从大语言模型到具身智能:百度百舸 LoongForge 入驻 AtomGit
人工智能·react native·百度·harmonyos·atomgit
梦想不只是梦与想7 小时前
鸿蒙 AI自动化测试实现
单元测试·harmonyos·hypium
用户09340777351413 小时前
HarmonyOS WPS Open SDK 工程实践:把 registerApp 做成就绪门禁
typescript·harmonyos
大雷神16 小时前
HarmonyOS7应用《拼棋派对》实战:双人键盘操作与窗口焦点恢复
harmonyos·arkui·harmonyos 7.0·玩转 arkts
2501_9462447817 小时前
开源鸿蒙平台 CMP 三方库「compose-continuous-rounded-corner-shape」适配全流程
华为·开源·harmonyos
KevinChaos18 小时前
灶台菜谱设计手记 03|HarmonyOS 7.0 空间化:沉浸光感反馈
harmonyos
大雷神18 小时前
HarmonyOS7应用《拼棋派对》实战:折叠屏与电脑的窗口布局适配
harmonyos·arkui·harmonyos 7.0·玩转 arkts
梦想不只是梦与想18 小时前
DevEco Studio:Debug界面按钮作用
鸿蒙·断点调试·debug调试
2501_9462447818 小时前
开源鸿蒙平台 CMP 三方库「Waterfall」适配全流程
华为·开源·harmonyos
2401_zq136y0318 小时前
开源鸿蒙平台 CMP 三方库「Jewel」适配全流程
华为·开源·harmonyos