字幕排序定位

2024-01-22 16:58 更新

可以與容器組件聯(lián)動用于按邏輯結(jié)構(gòu)快速定位容器顯示區(qū)域的組件。

說明

該組件從API Version 7開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。

子組件

接口

AlphabetIndexer(value: {arrayValue: Array<string>, selected: number})

參數(shù):

參數(shù)名

參數(shù)類型

必填

參數(shù)描述

arrayValue

Array<string>

字母索引字符串?dāng)?shù)組,不可設(shè)置為空。

selected

number

初始選中項索引值,若超出索引值范圍,則取默認(rèn)值0。

屬性

除支持通用屬性外,還支持以下屬性:

名稱

參數(shù)類型

描述

color

ResourceColor

設(shè)置文字顏色。

默認(rèn)值:0x99000000。

selectedColor

ResourceColor

設(shè)置選中項文字顏色。

默認(rèn)值:0xFF254FF7。

popupColor

ResourceColor

設(shè)置提示彈窗文字顏色。

默認(rèn)值:0xFF254FF7。

selectedBackgroundColor

ResourceColor

設(shè)置選中項背景顏色。

默認(rèn)值:0x1F0A59F7。

popupBackground

ResourceColor

設(shè)置提示彈窗背景色。

默認(rèn)值:0xFFFFFFFF。

usingPopup

boolean

設(shè)置是否使用提示彈窗。

默認(rèn)值:false。

selectedFont

Font

設(shè)置選中項文字樣式。

默認(rèn)值:

{

size:'12.0fp',

style:FontStyle.Normal,

weight:FontWeight.Normal,

family:'HarmonyOS Sans'

}

popupFont

Font

設(shè)置提示彈窗字體樣式。

默認(rèn)值:

{

size:'24.0vp',

style:FontStyle.Normal,

weight:FontWeight.Normal,

family:'HarmonyOS Sans'

}

font

Font

設(shè)置字母索引條默認(rèn)字體樣式。

默認(rèn)值:

{

size:'12.0fp',

style:FontStyle.Normal,

weight:FontWeight.Normal,

family:'HarmonyOS Sans'

}

itemSize

string | number

設(shè)置字母索引條字母區(qū)域大小,字母區(qū)域為正方形,即正方形邊長。不支持設(shè)置為百分比。

默認(rèn)值:24.0

單位:vp

alignStyle

IndexerAlign

設(shè)置字母索引條彈框的對齊樣式,支持彈窗顯示在索引條右側(cè)和左側(cè)。

默認(rèn)值:IndexerAlign.Right。

selected

number

設(shè)置選中項索引值。

默認(rèn)值:0。

popupPosition

Position

設(shè)置彈出窗口相對于索引器條上邊框中點(diǎn)的位置。

默認(rèn)值:{x:60.0, y:48.0}。

IndexerAlign枚舉說明

名稱

描述

Left

彈框顯示在索引條右側(cè)。

Right

彈框顯示在索引條左側(cè)。

事件

支持通用事件外,還支持以下事件:

名稱

功能描述

onSelected(callback: (index: number) => void)(deprecated)

索引條選中回調(diào),返回值為當(dāng)前選中索引。 從API Version 8開始廢棄,建議使用onSelect代替。

onSelect(callback: (index: number) => void)8+

索引條選中回調(diào),返回值為當(dāng)前選中索引。

onRequestPopupData(callback: (index: number) => Array<string>)8+

選中字母索引后,請求索引提示彈窗顯示內(nèi)容回調(diào)。

返回值:索引對應(yīng)的字符串?dāng)?shù)組,此字符串?dāng)?shù)組在彈窗中豎排顯示,字符串列表最多顯示5個,超出部分可以滑動顯示。

onPopupSelect(callback: (index: number) => void)8+

字母索引提示彈窗字符串列表選中回調(diào)。

示例

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct AlphabetIndexerSample {
  5. private arrayA: string[] = ['安']
  6. private arrayB: string[] = ['卜', '白', '包', '畢', '丙']
  7. private arrayC: string[] = ['曹', '成', '陳', '催']
  8. private arrayL: string[] = ['劉', '李', '樓', '梁', '雷', '呂', '柳', '盧']
  9. private value: string[] = ['#', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
  10. 'H', 'I', 'J', 'K', 'L', 'M', 'N',
  11. 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
  12. 'V', 'W', 'X', 'Y', 'Z']
  13. build() {
  14. Stack({ alignContent: Alignment.Start }) {
  15. Row() {
  16. List({ space: 20, initialIndex: 0 }) {
  17. ForEach(this.arrayA, (item) => {
  18. ListItem() {
  19. Text(item)
  20. .width('80%')
  21. .height('5%')
  22. .fontSize(30)
  23. .textAlign(TextAlign.Center)
  24. }.editable(true)
  25. }, item => item)
  26. ForEach(this.arrayB, (item) => {
  27. ListItem() {
  28. Text(item)
  29. .width('80%')
  30. .height('5%')
  31. .fontSize(30)
  32. .textAlign(TextAlign.Center)
  33. }.editable(true)
  34. }, item => item)
  35. ForEach(this.arrayC, (item) => {
  36. ListItem() {
  37. Text(item)
  38. .width('80%')
  39. .height('5%')
  40. .fontSize(30)
  41. .textAlign(TextAlign.Center)
  42. }.editable(true)
  43. }, item => item)
  44. ForEach(this.arrayL, (item) => {
  45. ListItem() {
  46. Text(item)
  47. .width('80%')
  48. .height('5%')
  49. .fontSize(30)
  50. .textAlign(TextAlign.Center)
  51. }.editable(true)
  52. }, item => item)
  53. }
  54. .width('50%')
  55. .height('100%')
  56. AlphabetIndexer({ arrayValue: this.value, selected: 0 })
  57. .selectedColor(0xFFFFFF) // 選中項文本顏色
  58. .popupColor(0xFFFAF0) // 彈出框文本顏色
  59. .selectedBackgroundColor(0xCCCCCC) // 選中項背景顏色
  60. .popupBackground(0xD2B48C) // 彈出框背景顏色
  61. .usingPopup(true) // 是否顯示彈出框
  62. .selectedFont({ size: 16, weight: FontWeight.Bolder }) // 選中項字體樣式
  63. .popupFont({ size: 30, weight: FontWeight.Bolder }) // 彈出框內(nèi)容的字體樣式
  64. .itemSize(28) // 每一項的尺寸大小
  65. .alignStyle(IndexerAlign.Left) // 彈出框在索引條右側(cè)彈出
  66. .onSelect((index: number) => {
  67. console.info(this.value[index] + ' Selected!')
  68. })
  69. .onRequestPopupData((index: number) => {
  70. if (this.value[index] == 'A') {
  71. return this.arrayA // 當(dāng)選中A時,彈出框里面的提示文本列表顯示A對應(yīng)的列表arrayA,選中B、C、L時也同樣
  72. } else if (this.value[index] == 'B') {
  73. return this.arrayB
  74. } else if (this.value[index] == 'C') {
  75. return this.arrayC
  76. } else if (this.value[index] == 'L') {
  77. return this.arrayL
  78. } else {
  79. return [] // 選中其余子母項時,提示文本列表為空
  80. }
  81. })
  82. .onPopupSelect((index: number) => {
  83. console.info('onPopupSelected:' + index)
  84. })
  85. }
  86. .width('100%')
  87. .height('100%')
  88. }
  89. }
  90. }

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號