鸿蒙应用开发实战【58】— 状态列表页StatusListPage开发

鸿蒙应用开发实战【58】--- 状态列表页StatusListPage开发

本文是「号码助手全栈开发系列」第 58 篇,持续更新中...

开源社区:https://openharmonycrossplatform.csdn.net


前言

StatusListPage 是按状态筛选的应用列表页,用户从首页的统计卡片点击进入(如"待换绑"),查看该状态下所有应用,并支持分类/卡号 chip 二次筛选。

本篇涵盖:路由接收 status 参数、按状态查询 listByStatus、Chip 筛选行(分类+卡号)、水平滚动 chips、空状态(无待处理事项)、getFilteredRows 二次过滤、与首页 onPageShow 联动。


一、页面结构

复制代码
┌──────────────────────┐
│  ‹   待换绑 · 3       │
├──────────────────────┤
│                      │
│  [全部][APP][网站]    │  ← 水平滚动 chips
│  [卡1][卡2]          │
│                      │
│  ┌──┐ 微信           │
│  │W │ APP   卡1      │
│  └──┘ 待换绑         │
│                      │
│  ┌──┐ 淘宝           │
│  │T │ APP   卡2      │
│  └──┘ 待换绑         │
└──────────────────────┘

二、路由参数

typescript 复制代码
aboutToAppear(): void {
  const params = this.getUIContext().getRouter().getParams() as Record<string, Object>
  if (params && typeof params['status'] === 'string') {
    this.statusLabel = params['status'] as string
  }
  this.loadData()
}

来自首页统计卡片的调用:

typescript 复制代码
// HomePage StatCard
.onClick(() => {
  this.navigate('pages/StatusListPage', { status: '待换绑' })
})

三、数据加载

typescript 复制代码
private async loadData(): Promise<void> {
  this.loading = true
  try {
    this.cards = await CardDao.listAll()
    const cardLabelMap = new Map<number, string>()
    for (const c of this.cards) {
      cardLabelMap.set(c.id ?? 0, c.label)
    }
    const bindings = await AppBindingDao.listByStatus(this.statusLabel as BindingStatus)
    this.rows = bindings.map(b => ({
      binding: b,
      cardLabel: cardLabelMap.get(b.card_id) ?? ''
    }))
  } finally {
    this.loading = false
  }
}

AppBindingDao.listByStatus:

typescript 复制代码
async listByStatus(status: BindingStatus): Promise<AppBindingEntity[]> {
  const predicates = new RdbPredicates('app_bindings')
  predicates.equalTo('status', status)
  predicates.orderByDesc('updated_at')
  // 查询 → 遍历 ResultSet → 返回 entities
}

四、Chip 筛选行

4.1 水平滚动

typescript 复制代码
Scroll() {
  Row({ space: 8 }) {
    // 分类 chips
    ForEach(['全部', 'APP', '网站', '小程序'], (cat, idx) => {
      Text(cat)
        .fontColor(this.chipCatIdx === idx ? '#FFFFFF' : AppColors.TEXT_2)
        .backgroundColor(this.chipCatIdx === idx ? AppColors.PRIMARY : AppColors.CARD_B)
        .linearGradient(this.chipCatIdx === idx
          ? { angle: 135, colors: [[AppColors.PRIMARY, 0], [AppColors.PRIMARY_2, 1]] }
          : { angle: 135, colors: [[AppColors.CARD_B, 0], [AppColors.CARD_B, 1]] })
        .onClick(() => this.chipCatIdx = idx)
    })

    // 卡号 chips
    ForEach(this.cards, (card, idx) => {
      Text(card.label)
        .fontColor(this.chipCardIdx === idx + 1 ? '#FFFFFF' : AppColors.TEXT_2)
        .backgroundColor(this.chipCardIdx === idx + 1 ? AppColors.PRIMARY : AppColors.CARD_B)
        .onClick(() => {
          this.chipCardIdx = this.chipCardIdx === idx + 1 ? 0 : idx + 1
        })
    })
  }
}
.scrollable(ScrollDirection.Horizontal)  // 水平滚动
.scrollBar(BarState.Off)                 // 隐藏滚动条

点击切换:再次点击已选中的 chip 可取消选择(恢复到 idx=0)。

4.2 二次过滤

typescript 复制代码
private getFilteredRows(): AppRow[] {
  const CATEGORIES = ['全部', 'APP', '网站', '小程序']
  return this.rows.filter(r => {
    const catOk = this.chipCatIdx === 0 || r.binding.category === CATEGORIES[this.chipCatIdx]
    const cardOk = this.chipCardIdx === 0 || r.cardLabel === this.cards[this.chipCardIdx - 1]?.label
    return catOk && cardOk
  })
}

Chip 维度 选项数组 idx=0 含义 点击切换行为
分类 '全部','APP','网站','小程序' 全部 chipCatIdx = idx
卡号 this.cards 动态 全部 chipCardIdx = 点击取消

五、空状态

当某状态没有应用时(所有待换绑的应用都已处理完),显示完成状态:

typescript 复制代码
Column() {
  Column() {
    Text('✓').fontSize(Size.font24).fontColor('#FFFFFF').fontWeight(800)
  }
  .width(Size.s88).height(Size.s88).borderRadius(Size.s44)
  .linearGradient({ angle: 135, colors: [[AppColors.PRIMARY, 0], [AppColors.PRIMARY_2, 1]] })
  .justifyContent(FlexAlign.Center).margin({ bottom: 20 })

  Text('太棒了,没有待处理事项')
    .fontSize(Size.font15).fontWeight(AppFonts.WEIGHT_BOLD).fontColor(AppColors.TEXT)

  Button('返回首页', { type: ButtonType.Normal })
    .onClick(() => this.getUIContext().getRouter().back())
}

小结

要点 说明
路由参数 from HomePage StatCard: status
按状态查询 listByStatus 过滤 + orderByDesc
Chip 行 水平 Scroll + 二次筛选
Chip 切换 再次点击取消选择
空状态 "太棒了"完成状态
onPageShow 从详情页返回后自动刷新

下一篇文章,我们开发备份页 BackupPage------数据导出、导入、备份文件管理。


如果这篇文章对你有帮助,欢迎点赞👍、收藏⭐、关注🔔,你的支持是我持续创作的动力!


相关资源:

相关推荐
码间留白17 小时前
AI 给的两个方案都被我否了:把 startIcon 与启动遮罩做成同一张图,鸿蒙 APP 冷启动零白屏
性能优化·uni-app·ai编程·harmonyos
二流小码农18 小时前
鸿蒙开发:证书只能创建3个,第4个应用怎么办
android·ios·harmonyos
威哥爱编程1 天前
模拟面试智能体开发实战 12:每篇都能跑不等于接起来能跑,端到端联调收官把十一块拼图合成一个能用的智能体
python·agent·harmonyos
威哥爱编程1 天前
模拟面试智能体开发实战 11:报告只躺在后端怎么复盘,把总评搬上手机还要适配大小屏
python·agent·harmonyos
我闪了个去1 天前
一个 Scroll 撑起整个首页:HarmonyOS 7 懒加载布局把网格、瀑布流、吸顶分组装进一次滚动
harmonyos·arkts
二流小码农2 天前
鸿蒙开发:router组件升级,彻底告别手动配置
android·ios·harmonyos
威哥爱编程2 天前
模拟面试智能体开发实战 09:把面试官装进手机,鸿蒙端聊天界面用 SSE 流式输出让回复逐字滚出来
python·agent·harmonyos
威哥爱编程2 天前
模拟面试智能体开发实战 10:配置写死在代码里还怎么换服务端,把设置和简历做成界面可配、可存
python·agent·harmonyos
Sirius_Ji3 天前
# 一份鸿蒙原生支持硬解的 ffmpeg 开发基座
harmonyos
贾伟康3 天前
HarmonyOS API 26 Navigation路由栈上限:stackSizeLimit、过载保护与容量观测
harmonyos·arkts·navigation·navpathstack·api26