首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >纯血鸿蒙APP实战开发——城市定位选择案例

纯血鸿蒙APP实战开发——城市定位选择案例

原创
作者头像
小帅聊鸿蒙
发布2024-12-31 16:32:07
发布2024-12-31 16:32:07
7320
举报
文章被收录于专栏:鸿蒙开发笔记鸿蒙开发笔记

介绍

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

效果图预览

使用说明

分两个功能

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

实现思路

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

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

  • 通过AlphabetIndexer的selected属性与城市列表中List组件onScrollIndex事件绑定,CityView.ets
代码语言:ts
复制
AlphabetIndexer({ arrayValue: TAB_VALUE, selected: this.stabIndex })
  .height('100%')
  .selectedColor($r('app.color.citysearch_alphabet_select_color')) // 选中项文本颜色
  .popupColor($r('app.color.citysearch_alphabet_pop_color')) // 弹出框文本颜色
  .selectedBackgroundColor($r('app.color.citysearch_alphabet_selected_bgc')) // 选中项背景颜色
  .popupBackground($r('app.color.citysearch_alphabet_pop_bgc')) // 弹出框背景颜色
  .popupPosition({ x: $r('app.integer.citysearch_citysearch_pop_position_x'), y: $r('app.integer.citysearch_citysearch_pop_position_y') })
  .usingPopup(true) // 是否显示弹出框
  .selectedFont({ size: $r('app.integer.citysearch_select_font'), weight: FontWeight.Bolder }) // 选中项字体样式
  .popupFont({ size: $r('app.integer.citysearch_pop_font'), weight: FontWeight.Bolder }) // 弹出框内容的字体样式
  .alignStyle(IndexerAlign.Right) // 弹出框在索引条左侧弹出
  .onSelect((tabIndex: number) => {
    this.scroller.scrollToIndex(tabIndex);
  })
  • 当用户滑动List组件,list组件onScrollIndex监听到firstIndex的改变,绑定赋值给AlphabetIndexer的selected属性,从而定位到字母索引。
  • 当点击AlphabetIndexer的字母索引时,通过scrollToIndex触发list组件滑动并指定firstIndex,从而实现List列表与AlphabetIndexer组件首字母联动吸顶展示,CityView.ets。
代码语言:ts
复制
List({ space: 14, initialIndex: 0, scroller: this.scroller }) {
  .onScrollIndex((firstIndex: number, lastIndex: number) => {
     this.stabIndex = firstIndex;
  })
}

高性能知识点

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

工程结构&模块类型

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

目录
  • 介绍
  • 效果图预览
  • 实现思路
    • 场景:通过AlphabetIndexer实现索引条导航
  • 高性能知识点
  • 工程结构&模块类型
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档