当前位置:   article > 正文

ArkTS List刷新行的方式一

ArkTS List刷新行的方式一

通过动态改变 RowState 的 state 属性,来实现刷新。代码如下:

  1. // 控件类型
  2. export enum ControlType {
  3. Label = 1, // 文本标签
  4. TextBox = 2, // 单行文本框
  5. }
  6. @Entry
  7. @Component
  8. struct TestAct {
  9. rowRootData: RowRootData = null;
  10. aboutToAppear() {
  11. this.rowRootData = getTestDatas();
  12. }
  13. build() {
  14. List({ space: 10 }) {
  15. ForEach(this.rowRootData.dataList, (rowData: RowData, index?: number) => {
  16. ListItem() {
  17. this.getRowWidget(rowData, index)
  18. }
  19. .width('100%')
  20. })
  21. }
  22. }
  23. @Builder getRowWidget(rowData: RowData, index: number) {
  24. RowView({
  25. rowRootData: this.rowRootData,
  26. myIndex: index,
  27. rowData: rowData,
  28. })
  29. }
  30. }
  31. /**
  32. * 注意事项:
  33. * * 1. 参数 rowRootData, myIndex 为必传
  34. * * 2. 参数 rowData 可传可不传
  35. */
  36. @Component
  37. struct RowView {
  38. // 为了性能,尽量不让 rowRootData 使用 @Provide装饰器 和 @Consume装饰器
  39. rowRootData: RowRootData;
  40. myIndex: number;
  41. @State rowData: RowData = null;
  42. @State rowState: RowState = new RowState();
  43. aboutToAppear() {
  44. this.rowRootData.setRowState(this.myIndex, this.rowState);
  45. if (this.rowData == null) this.rowData = this.rowRootData.getDataByIndex(this.myIndex);
  46. }
  47. build() {
  48. Column() {
  49. if (this.rowState.state) {
  50. this.buildWidget();
  51. } else {
  52. this.buildWidget();
  53. }
  54. }
  55. }
  56. @Builder buildWidget() {
  57. Column() {
  58. if (ControlType.TextBox == this.rowData.data["controlType"]) {
  59. TextArea({ placeholder: this.rowData.data["name1"], text: this.rowData.data["code1"] })
  60. .placeholderColor('#00bbcc')
  61. .placeholderFont({ size: 26 })
  62. .align(Alignment.Center)
  63. .textAlign(TextAlign.Center)
  64. .size({ width: '100%', height: 60 })
  65. .fontSize(26)
  66. .backgroundColor('#aabbcc')
  67. .onChange((text) => {
  68. this.rowData.data["code1"] = text;
  69. })
  70. .onClick(() => {
  71. this.notificationRefresh();
  72. })
  73. } else {
  74. Text(this.rowData.data["name1"])
  75. .size({ width: '100%', height: 60 })
  76. .fontSize(26)
  77. .backgroundColor('#bbbb00')
  78. }
  79. }.onClick(() => {
  80. this.notificationRefresh();
  81. })
  82. }
  83. notificationRefresh() {
  84. this.rowRootData.refreshAll(this.myIndex);
  85. }
  86. }
  87. export class RowRootData {
  88. private _dataList: Array<RowData>;
  89. private rowStateList: Array<RowState>;
  90. public get dataList(): Array<RowData> {
  91. return this._dataList;
  92. }
  93. public set dataList(dataList: Array<RowData>) {
  94. this._dataList = dataList;
  95. this.rowStateList = new Array<RowState>(this._dataList.length);
  96. }
  97. public getDataByIndex(index: number): RowData {
  98. return this._dataList[index];
  99. }
  100. public setRowState(index: number, rowState: RowState) {
  101. this.rowStateList[index] = rowState;
  102. }
  103. public refreshAll(excludeIndex: number = -1) {
  104. for (let i = 0, end = this.rowStateList.length; i < end; i++) {
  105. if (i != excludeIndex) {
  106. this.rowStateList[i].toggle();
  107. }
  108. }
  109. }
  110. public refreshAllByData(rowData?: RowData) {
  111. this.refreshAll(this._dataList.indexOf(rowData));
  112. }
  113. }
  114. export class RowData {
  115. readonly data: Object;
  116. constructor(data: Object) {
  117. this.data = data;
  118. }
  119. }
  120. class RowState {
  121. state: boolean;
  122. constructor() {
  123. this.state = false;
  124. }
  125. public toggle() {
  126. this.state = !this.state;
  127. }
  128. }
  129. function getTestDatas(end: number = 20): RowRootData {
  130. let dataList: Array<RowData> = new Array<RowData>(end);
  131. let result = new RowRootData();
  132. for (let i = 0; i < end; i++) {
  133. dataList[i] = new RowData({
  134. name1: "Name " + (i + 1),
  135. code1: "",
  136. controlType: ((i & 1) == 1) ? ControlType.Label : ControlType.TextBox,
  137. });
  138. }
  139. result.dataList = dataList;
  140. return result;
  141. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号