1
0

ListViewAdpater.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. class ListViewAdapter {
  2. constructor(scrollView) {
  3. this.scrollView = scrollView;
  4. this.content = scrollView.content;
  5. this.scriptItems = [];
  6. this.dataList = [];
  7. // this.initialize();
  8. this.updateTimer = 0;
  9. this.updateInterval = 0.1;
  10. this.lastContentPosY = 0; // use this variable to detect if we are scrolling up or down
  11. this.spawnCount = 16;
  12. this.itemHeight = 0;
  13. this.bufferZone = 500;
  14. this.spacing = 10;
  15. this.scriptName = '';
  16. this.fuckingItem = null;
  17. }
  18. initialize(itemTemplate) {
  19. console.log(this.scriptName);
  20. let actualCount = this.dataList.length < this.spawnCount ? this.dataList.length : this.spawnCount;
  21. // let actualCount = this.dataList.length;
  22. this.scriptItems = [];
  23. this.content.removeAllChildren();
  24. for (let i = 0; i < actualCount; ++i) { // spawn items, we only need to do this once
  25. let item = cc.instantiate(itemTemplate);
  26. if (this.itemHeight === 0) {
  27. this.itemHeight = item.height; // get total content height
  28. this.content.height = (this.itemHeight + this.spacing) * this.dataList.length;
  29. }
  30. this.content.addChild(item);
  31. item.setPosition(0, -item.height * (0.5 + i) - this.spacing * (i + 1));
  32. let itemScript = item.getComponent(this.scriptName);
  33. // itemScript.setListViewAdapter(this);
  34. itemScript.updateItem(this.dataList[i], i);
  35. this.scriptItems.push(itemScript);
  36. }
  37. }
  38. getPositionInView(item) { // get item position in scrollview's node space
  39. let worldPos = item.parent.convertToWorldSpaceAR(item.position);
  40. let viewPos = this.scrollView.node.convertToNodeSpaceAR(worldPos);
  41. return viewPos;
  42. }
  43. update(dt) {
  44. this.updateTimer += dt;
  45. if (this.updateTimer < this.updateInterval) return; // we don't need to do the math every frame
  46. this.updateTimer = 0;
  47. let buffer = this.bufferZone;
  48. let isDown = this.content.y < this.lastContentPosY; // scrolling direction
  49. let offset = (this.itemHeight + this.spacing) * this.scriptItems.length;
  50. for (let i = 0; i < this.scriptItems.length; ++i) {
  51. let viewPos = this.getPositionInView(this.scriptItems[i].node);
  52. if (isDown) {
  53. // if away from buffer zone and not reaching top of content
  54. if (viewPos.y < -buffer && this.scriptItems[i].y + offset < 0) {
  55. this.scriptItems[i].node.setPositionY(this.scriptItems[i].y + offset);
  56. let itemScript = this.scriptItems[i];
  57. let itemId = itemScript._itemId - this.scriptItems.length;
  58. itemScript.updateItem(this.dataList[itemId], itemId);
  59. }
  60. } else {
  61. // if away from buffer zone and not reaching bottom of content
  62. if (viewPos.y > buffer && this.scriptItems[i].y - offset > -this.content.height) {
  63. this.scriptItems[i].node.setPositionY(this.scriptItems[i].y - offset);
  64. let itemScript = this.scriptItems[i];
  65. let itemId = itemScript._itemId + scriptItems.length;
  66. itemScript.updateItem(this.dataList[itemId], itemId);
  67. if (itemId === (this.dataList.length - 1)) {
  68. if (this.loadMore != undefined) {
  69. this.loadMore();
  70. }
  71. }
  72. }
  73. }
  74. }
  75. // update lastContentPosY
  76. this.lastContentPosY = this.content.y;
  77. }
  78. updateItems(dataList, itemPrefab, scriptName) {
  79. this.scriptName = scriptName;
  80. this.itemTemplate = itemPrefab;
  81. this.dataList = dataList;
  82. this.initialize(itemPrefab);
  83. }
  84. addData(newdataList) {
  85. this.dataList = this.dataList.concat(newdataList);
  86. this.content.height = (this.itemHeight + this.spacing) * this.dataList.length; // get total content height
  87. }
  88. setLoadMoreCallBack(loadMore) {
  89. this.loadMore = loadMore;
  90. }
  91. playRemoveItemAnimation(itemScript, animCallback) {
  92. this.dataList.splice(itemScript._itemId, 1);
  93. this.content.height = (this.itemHeight + this.spacing) * this.dataList.length; // get total content height
  94. let sequ = cc.sequence(cc.moveBy(0.2, -750, 0), animCallback);
  95. itemScript.node.runAction(sequ);
  96. }
  97. updateItemsAndDataList(itemScript) {
  98. let index = itemScript._itemId;
  99. let scriptItems = this.scriptItems;
  100. if (this.dataList.length >= scriptItems.length) {
  101. itemScript.scheduleOnce(() => {
  102. itemScript.node.x = 0;
  103. }, 0.2);
  104. for (let i = 0; i < scriptItems.length; ++i) {
  105. let itemNode = scriptItems[i].node;
  106. let script = scriptItems[i];
  107. let itemId = script._itemId;
  108. if (itemId < this.dataList.length) {
  109. if (script._itemId > index) {
  110. let finish = cc.callFunc(() => {
  111. itemNode.y = itemNode.y - (this.itemHeight + this.spacing);
  112. script.updateItem(this.dataList[itemId], itemId);
  113. }, this);
  114. let s = cc.sequence(cc.moveBy(0.2, 0, (this.itemHeight + this.spacing)), finish);
  115. itemNode.runAction(s);
  116. } else {
  117. itemScript.scheduleOnce(() => {
  118. script.updateItem(this.dataList[itemId], itemId);
  119. }, 0.2);
  120. }
  121. }
  122. }
  123. } else {
  124. for (let i = 0; i < scriptItems.length; ++i) {
  125. let itemId = scriptItems[i]._itemId;
  126. if (itemId === itemScript._itemId) {
  127. this.scriptItems.splice(i, 1);
  128. }
  129. }
  130. for (let i = 0; i < scriptItems.length; ++i) {
  131. let itemNode = scriptItems[i].node;
  132. let script = scriptItems[i];
  133. if (script._itemId > index) {
  134. let newId = script._itemId - 1;
  135. itemNode.runAction(cc.moveBy(0.2, 0, (this.itemHeight + this.spacing)));
  136. script.updateItem(this.dataList[newId], newId);
  137. }
  138. }
  139. itemScript.node.destroy();
  140. }
  141. }
  142. removeItem(itemScript) {
  143. let finish = cc.callFunc(() => {
  144. this.updateItemsAndDataList(itemScript);
  145. });
  146. this.playRemoveItemAnimation(itemScript, finish);
  147. }
  148. onDestroy() {
  149. this.scriptItems = [];
  150. this.content.removeAllChildren();
  151. }
  152. }
  153. module.exports = ListViewAdapter;