使用AlphabetIndexer组件实现建议通讯录页面

实现一个简易的通讯录功能,可以实现快捷跳转以及滚动刷新的功能。该功能用到的场景较多,比如通讯录、页签、列表分类等多类场景。

HarmonyOS
2024-05-26 14:24:50
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
伊普洛先生

实现基本原理:通过List组件渲染通讯录中联系人的信息,通过AlphabetIndexer组件实现右侧的快速定位容器栏,然后将两者数据进行绑定实现联动效果。

代码如下:

class Contacts { 
  constructor(index: string, names: string[]) { 
    this.index = index 
    this.names = names 
  } 
 
  index: string // 序号 
  names: string[] // 姓名组 
} 
 
@Entry 
@Component 
struct Index { 
  @State contacts: Contacts[] = [ 
    new Contacts('A', ["a", "aa", "aaa"]), 
    new Contacts('B', ["b", "bbb", "bb"]), 
    new Contacts('C', ["c", "cc", "ccc"]), 
    new Contacts('D', ["d", "dd", "ddd"]), 
    new Contacts('E', ["e", "ee"]), 
    new Contacts('F', ["f", "ff"]), 
    new Contacts('G', ["g", "gg", "ggg"]), 
    new Contacts('H', ["h", "hh", "hhh"]), 
  ] 
  scroller: Scroller = new Scroller() // list 滚动控制 
  value: string[] = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] 
  @State flag: number = 0 // 绑定AlphabetIndexer的index 
  preIndex: number = 0; 
 
  build() { 
    Row() { 
      List({ space: 20, initialIndex: 0, scroller: this.scroller }) { 
        ForEach(this.contacts, (value: Contacts) => { 
          ListItem() { 
            Column() { 
              Text('' + value.index) 
                .fontSize(30) 
                .textAlign(TextAlign.Center) 
                .backgroundColor('#ffffff') 
                .margin(30) 
              ForEach(value.names, (name: string) => { 
                Column({ space: 10 }) { 
                  Text('' + name) 
                    .fontSize(20) 
                    .textAlign(TextAlign.Center) 
                    .backgroundColor('#ffffff') 
                    .margin(10) 
                } 
              }) 
            } 
          } 
        }) 
      } 
      .width('80%') 
      .backgroundColor('#fffff0') 
      .listDirection(Axis.Vertical) 
      .onScrollIndex((firstIndex: number, lastIndex: number) => { 
        this.flag = firstIndex // 刷新 AlphabetIndexer 
      }) 
 
      AlphabetIndexer({ arrayValue: this.value, selected: this.flag }) 
        .width("20%") 
        .font({ size: 20, weight: FontWeight.Bold }) 
        .selectedColor(Color.Blue) 
        .selectedBackgroundColor(0xCCCCCC) 
        .usingPopup(true)// 是否显示弹出框 
        .selectedFont({ size: 28, weight: FontWeight.Bolder }) 
        .itemSize(30) 
        .alignStyle(IndexerAlign.Left) 
        .onSelect((index: number) => { 
          console.info(`AlphabetIndexer 选中了 ${this.value[index]}`) 
          for (let i = 0; i < this.contacts.length; i++) { 
            if (this.contacts[i].index.toString() === this.value[index]) { 
              this.preIndex = index; 
              this.scroller.scrollToIndex(i); 
              return; 
            } 
          } 
          this.flag = this.preIndex; 
        }) 
    }.width("100%") 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
分享
微博
QQ
微信
回复
2024-05-27 18:14:03
相关问题
HarmonyOS 读取通讯录,保存通讯录
1168浏览 • 1回复 待解决
HarmonyOS 添加通讯录功能
932浏览 • 1回复 待解决
如何跳转通讯录 、 打电话
2760浏览 • 1回复 待解决
HarmonyOS NEXT(4.0)通讯录权限申请
1065浏览 • 1回复 待解决
HarmonyOS 通讯录与数据库问题
1096浏览 • 1回复 待解决
HarmonyOS 添加联系人到通讯录没反应
991浏览 • 1回复 待解决
HarmonyOS 通讯录相关功能布局实现
1135浏览 • 1回复 待解决
HarmonyOS AlphabetIndexer组件问题
682浏览 • 1回复 待解决
建议开发使用什么单位
2607浏览 • 1回复 待解决
HarmonyOS 关于SegmentButton组件建议
834浏览 • 1回复 待解决
通过emitter实现worker间线程通讯
1832浏览 • 1回复 待解决