当前位置:   article > 正文

ArkTS List刷新行的方式二_arkts 列表 刷新

arkts 列表 刷新

List 是通过 LazyForEach 来实现的;通过改变 keyGenertator(LazyForEach中的参数)的返回值,来实现刷新行的效果。代码如下:

  1. @Entry
  2. @Component
  3. struct ListAct {
  4. @State baseAdapter: MyAdapter = null;
  5. private scroller: Scroller = new Scroller();
  6. aboutToAppear() {
  7. this.initDatas();
  8. }
  9. initDatas() {
  10. let list: ListDataItem[] = new Array<ListDataItem>(100);
  11. for (let i = 0, end = list.length; i < end; i++) {
  12. list[i] = new ListDataItem("条目:" + (i + 1));
  13. }
  14. this.baseAdapter = new MyAdapter(list);
  15. }
  16. addDatas(index: number, count: number) {
  17. let list: ListDataItem[] = new Array<ListDataItem>(count);
  18. for (let i = 0, end = count; i < end; i++) {
  19. list[i] = new ListDataItem("新增条目:" + (i + 1));
  20. }
  21. this.baseAdapter.addDatas(list, index);
  22. }
  23. refreshAllDatas() {
  24. let list: ListDataItem[] = new Array<ListDataItem>(20);
  25. for (let i = 0, end = list.length; i < end; i++) {
  26. list[i] = new ListDataItem("刷新条目:" + (i + 1));
  27. }
  28. this.baseAdapter.refreshDatas(list);
  29. }
  30. @Builder itemEnd(item: ListDataItem, index: number) {
  31. // 侧滑后尾端出现的组件
  32. Button({ type: ButtonType.Normal }) {
  33. Row() {
  34. Row() {
  35. Image($r('app.media.app_icon'))
  36. .width(20)
  37. .height(20)
  38. .objectFit(ImageFit.Contain)
  39. .margin({ left: 16, right: 16 })
  40. Text("修改")
  41. }.onClick(() => {
  42. item.name = "修改" + item.name;
  43. // this.baseAdapter.refreshDataByIndex(index);
  44. this.baseAdapter.refreshData(item);
  45. })
  46. Row() {
  47. Image($r('app.media.app_icon'))
  48. .width(20)
  49. .height(20)
  50. .objectFit(ImageFit.Contain)
  51. .margin({ left: 16, right: 16 })
  52. Text("删除")
  53. }.onClick(() => {
  54. // this.baseAdapter.removeDataByIndex(index);
  55. this.baseAdapter.removeData(item);
  56. })
  57. }
  58. }.height(40)
  59. }
  60. build() {
  61. Column() {
  62. Row() {
  63. Button("添加数据").onClick(() => {
  64. this.addDatas(0, 2);
  65. })
  66. Button("刷新数据").onClick(() => {
  67. this.refreshAllDatas();
  68. })
  69. }
  70. List({ space: 8, scroller: this.scroller }) {
  71. LazyForEach(this.baseAdapter, (item: ListDataItem, index) => {
  72. ListItem() {
  73. if (item) {
  74. Row() {
  75. Text(item.name).margin(24)
  76. }.width("100%").align(Alignment.Start)
  77. }
  78. }
  79. .swipeAction({ end: this.itemEnd.bind(this, item, index) }) // 设置侧滑属性
  80. }, (item: ListDataItem, index) => item.getKey())
  81. }
  82. .backgroundColor('#eeeeee')
  83. .divider({ strokeWidth: 1, color: 0x222222 })
  84. .layoutWeight(1)
  85. }
  86. }
  87. }
  88. class ListDataItem {
  89. name: string;
  90. private key: string = null;
  91. constructor(name: string) {
  92. this.name = name;
  93. }
  94. public clearKey() {
  95. this.key = null;
  96. }
  97. public getKey() {
  98. if (this.key == null) {
  99. this.key = uuid();
  100. }
  101. return this.key;
  102. }
  103. }
  104. class BaseAdapter <T> implements IDataSource {
  105. private listeners: DataChangeListener[] = new Array<DataChangeListener>();
  106. protected dataArray: T[];
  107. constructor(dataArray: T[]) {
  108. this.dataArray = dataArray ? dataArray : [];
  109. }
  110. public refreshDatas(dataArray: T[]) {
  111. this.dataArray = dataArray;
  112. this.notifyDataReload();
  113. }
  114. notifyDataReload(): void {
  115. this.listeners.forEach(listener => {
  116. listener.onDataReloaded();
  117. })
  118. }
  119. registerDataChangeListener(listener: DataChangeListener): void {
  120. if (this.listeners.indexOf(listener) < 0) {
  121. this.listeners.push(listener);
  122. }
  123. }
  124. unregisterDataChangeListener(listener: DataChangeListener): void {
  125. const pos = this.listeners.indexOf(listener);
  126. if (pos >= 0) {
  127. this.listeners.splice(pos, 1);
  128. }
  129. }
  130. public totalCount(): number {
  131. return this.dataArray.length;
  132. }
  133. public getData(index: number): T {
  134. return this.dataArray[index];
  135. }
  136. public getCount(): number {
  137. return this.dataArray.length;
  138. }
  139. }
  140. class MyAdapter extends BaseAdapter<ListDataItem> {
  141. constructor(dataArray: ListDataItem[]) {
  142. super(dataArray);
  143. }
  144. public refreshData(t: ListDataItem) {
  145. this.refreshDataByIndex(this.dataArray.indexOf(t));
  146. }
  147. public refreshDataByIndex(index: number) {
  148. if (index >= 0 && index < this.dataArray.length) {
  149. this.dataArray[index].clearKey();
  150. this.notifyDataReload();
  151. }
  152. }
  153. public addData(t: ListDataItem, index: number = -1): void {
  154. for (let item of this.dataArray) item.clearKey();
  155. if (index < 0 && index >= this.dataArray.length) {
  156. this.dataArray.push(t);
  157. } else {
  158. this.dataArray.splice(index, 0, t);
  159. }
  160. this.notifyDataReload();
  161. }
  162. public addDatas(ts: ListDataItem[], index: number = -1): void {
  163. if (ts && ts.length > 0) {
  164. for (let item of this.dataArray) item.clearKey();
  165. if (index >= 0 && index < this.dataArray.length) {
  166. this.dataArray.splice(index, 0, ...ts);
  167. } else {
  168. index = this.dataArray.length;
  169. this.dataArray.splice(index, 0, ...ts);
  170. }
  171. this.notifyDataReload();
  172. }
  173. }
  174. public removeData(t: ListDataItem) {
  175. this.removeDataByIndex(this.dataArray.indexOf(t));
  176. }
  177. public removeDataByIndex(index: number) {
  178. if (index >= 0 && index < this.dataArray.length) {
  179. this.dataArray.splice(index, 1)
  180. for (let item of this.dataArray) item.clearKey();
  181. this.notifyDataReload();
  182. }
  183. }
  184. }
  185. function uuid(): string {
  186. var uuidValue = "", k, randomValue;
  187. for (k = 0; k < 32; k++) {
  188. randomValue = Math.random() * 16 | 0;
  189. if (k == 8 || k == 12 || k == 16 || k == 20) {
  190. uuidValue += "-"
  191. }
  192. uuidValue += (k == 12 ? 4 : (k == 16 ? (randomValue & 3 | 8) : randomValue)).toString(16);
  193. }
  194. return uuidValue;
  195. }

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

闽ICP备14008679号