鸿蒙实战:HarmonyOS NEXT【城市选择】

本文介绍了如何使用AlphabetIndexer组件实现在城市选择场景中的拼音首字母快速定位,通过onScrollIndex事件实现List组件与AlphabetIndexer的联动,同时提到高性能优化和工程结构模块依赖。
摘要由CSDN通过智能技术生成

城市选择案例

介绍

本示例介绍城市选择场景的使用:通过[AlphabetIndexer]实现首字母快速定位城市的索引条导航。

效果图预览

使用说明

分两个功能

  • 在搜索框中可以根据城市拼音模糊搜索出相近的城市,例如输入"a",会出现"阿尔山"、“阿勒泰地区”、“安庆”、“安阳”。
  • 下方城市列表通过AlphabetIndexer组件实现拼音索引条,通过滑动选择城市首拼,快速定位相关首拼城市。

实现思路

场景:通过AlphabetIndexer实现索引条导航

城市列表中的右侧首拼索引条,通过AlphabetIndexer组件实现首字母快速定位城市的索引条导航。

  • 通过AlphabetIndexer的selected属性与城市列表中List组件onScrollIndex事件绑定
AlphabetIndexer({ arrayValue: TAB_VALUE, selected: this.stabIndex })
  .height('100%')
  .selectedColor($r('app.color.alphabet_select_color')) // 选中项文本颜色
  .popupColor($r('app.color.alphabet_pop_color')) // 弹出框文本颜色
  .selectedBackgroundColor($r('app.color.alphabet_selected_bgc')) // 选中项背景颜色
  .popupBackground($r('app.color.alphabet_pop_bgc')) // 弹出框背景颜色
  .popupPosition({ x: $r('app.integer.pop_position_x'), y: $r('app.integer.pop_position_y') })
  .usingPopup(true) // 是否显示弹出框
  .selectedFont({ size: $r('app.integer.select_font'), weight: FontWeight.Bolder }) // 选中项字体样式
  .popupFont({ size: $r('app.integer.pop_font'), weight: FontWeight.Bolder }) // 弹出框内容的字体样式
  .alignStyle(IndexerAlign.Right) // 弹出框在索引条左侧弹出
  .onSelect((tabIndex: number) => {
    this.scroller.scrollToIndex(tabIndex);
  })
  • 当用户划入或划出List组件显示区域时触发,通过将onScrollIndex拿到的首字母firstIndex绑定赋值给AlphabetIndexer的selected属性,
    从而实现List列表与AlphabetIndexer组件首字母联动吸顶展示。
List({ space: 14, initialIndex: 0, scroller: this.scroller }) {
  .onScrollIndex((firstIndex: number, lastIndex: number) => {
     this.stabIndex = firstIndex;
  })
}

高性能知识点

由于需要通过搜索按钮频繁的控制自定义组件的显隐状态,因此推荐使用显隐控制替代条件渲染。

工程结构&模块类型

citysearch                                      // har类型
|---src/main/ets/view
|   |---CitySearch.ets                          // 视图层-主页 
|   |---CityView.ets                            // 视图层-城市列表组件
|   |---SearchView.ets                          // 视图层-搜索组件
|---src/main/ets/model
|   |---DetailData.ets                          // 模型层-数据模块

模块依赖

依赖[har包-common库中UX标准]

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值