1
0

ListViewAdapter.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 = 7; //生成多少个预制资源
  12. this.itemHeight = 0;
  13. this.bufferZone = 500;
  14. this.spacing = 0; //两个item之间的空隙
  15. this.scriptName = '';
  16. this.topHeight = 0; //顶部空间
  17. this.shouldRemove = true; //初始化前是否需要清除所有节点
  18. this.fuckingItem = null;
  19. }
  20. initialize(itemTemplate) {
  21. this.actualCount = this.dataList.length < this.spawnCount ? this.dataList.length : this.spawnCount;
  22. // let actualCount = this.dataList.length;
  23. this.scriptItems = [];
  24. if (this.itemHeight > 0) {
  25. this.content.height = (this.itemHeight + this.spacing) * this.dataList.length + this.topHeight;
  26. }
  27. if (this.shouldRemove) {
  28. this.content.removeAllChildren();
  29. }
  30. for (let i = 0; i < this.actualCount; ++i) { // spawn items, we only need to do this once
  31. let item = cc.instantiate(itemTemplate);
  32. if (this.itemHeight === 0) {
  33. this.itemHeight = item.height; // get total content height
  34. this.content.height = (this.itemHeight + this.spacing) * this.dataList.length + this.topHeight;
  35. }
  36. this.content.addChild(item);
  37. item.setPosition(0, -(item.height * (0.5 + i) + this.spacing * (i + 1) + this.topHeight));
  38. let itemScript = item.getComponent(this.scriptName);
  39. itemScript.setListViewAdapter(this);
  40. itemScript.updateItem(this.dataList[i], i, (newBuildingInfo, newItemId) => {
  41. this.dataList[newItemId] = newBuildingInfo;
  42. });
  43. this.scriptItems.push(itemScript);
  44. // console.log('content height: ', this.content.height);
  45. }
  46. this.bufferZone = (this.scrollView.node.height + this.itemHeight) / 2;
  47. //第一次数据加载完成后发出回调
  48. if (this.loadFinish != undefined) {
  49. this.loadFinish();
  50. }
  51. }
  52. getPositionInView(item) { // get item position in scrollview's node space
  53. let worldPos = item.parent.convertToWorldSpaceAR(item.position);
  54. let viewPos = this.scrollView.node.convertToNodeSpaceAR(worldPos);
  55. return viewPos;
  56. }
  57. update(dt) {
  58. // this.updateTimer += dt;
  59. // if (this.updateTimer < this.updateInterval) return; // we don't need to do the math every frame
  60. this.updateTimer = 0;
  61. let buffer = this.bufferZone;
  62. let isDown = this.content.y < this.lastContentPosY; // scrolling direction
  63. let offset = (this.itemHeight + this.spacing) * this.scriptItems.length;
  64. for (let i = 0; i < this.scriptItems.length; ++i) {
  65. let viewPos = this.getPositionInView(this.scriptItems[i].node);
  66. if (isDown) {
  67. // if away from buffer zone and not reaching top of content
  68. if (viewPos.y < -buffer && this.scriptItems[i].node.y + offset < 0) {
  69. // this.scriptItems[i].node.setPositionY(this.scriptItems[i].node.y + offset);
  70. let itemScript = this.scriptItems[i];
  71. let itemId = itemScript._itemId - this.scriptItems.length;
  72. if (itemId < 0) {
  73. // this.scriptItems[i].node.active = false;
  74. continue;
  75. }
  76. // this.scriptItems[i].node.active = true;
  77. this.scriptItems[i].node.y = this.scriptItems[i].node.y + offset;
  78. itemScript.updateItem(this.dataList[itemId], itemId, (newBuildingInfo, newItemId) => {
  79. this.dataList[newItemId] = newBuildingInfo;
  80. });
  81. }
  82. } else {
  83. // if away from buffer zone and not reaching bottom of content
  84. if (viewPos.y > buffer && this.scriptItems[i].node.y - offset > -this.content.height) {
  85. // this.scriptItems[i].node.setPositionY(this.scriptItems[i].node.y - offset);
  86. let itemScript = this.scriptItems[i];
  87. let itemId = itemScript._itemId + this.scriptItems.length;
  88. if (itemId >= this.dataList.length) {
  89. // this.scriptItems[i].node.active = false;
  90. continue;
  91. }
  92. // this.scriptItems[i].node.active = true;
  93. this.scriptItems[i].node.y = this.scriptItems[i].node.y - offset;
  94. itemScript.updateItem(this.dataList[itemId], itemId, (newBuildingInfo, newItemId) => {
  95. this.dataList[newItemId] = newBuildingInfo;
  96. });
  97. if (itemId === (this.dataList.length - 1)) {
  98. if (this.loadMore != undefined) {
  99. this.loadMore();
  100. }
  101. }
  102. }
  103. }
  104. }
  105. // update lastContentPosY
  106. this.lastContentPosY = this.content.y;
  107. }
  108. updateItems(dataList, itemPrefab, scriptName, topHeight) {
  109. this.scriptName = scriptName;
  110. this.itemPrefab = itemPrefab;
  111. this.dataList = dataList;
  112. this.topHeight = (topHeight === undefined) ? 0 : topHeight;
  113. this.initialize(itemPrefab);
  114. }
  115. //更新数据
  116. updateDataList(dataList, roomId) {
  117. this.dataList = dataList;
  118. for (let i = 0; i < this.scriptItems.length; ++i) {
  119. let itemScript = this.scriptItems[i];
  120. if (itemScript.data.roomId == roomId) {
  121. let itemId = itemScript._itemId;
  122. itemScript.updateItem(this.dataList[itemId], itemId, (newBuildingInfo, newItemId) => {
  123. this.dataList[newItemId] = newBuildingInfo;
  124. });
  125. }
  126. }
  127. }
  128. //刷新已有数据
  129. refreshItems(dataList) {
  130. this.dataList = dataList;
  131. this.update();
  132. }
  133. addData(newdataList) {
  134. this.dataList = this.dataList.concat(newdataList);
  135. this.content.height = (this.itemHeight + this.spacing) * this.dataList.length; // get total content height
  136. }
  137. setLoadMoreCallBack(loadMore) {
  138. this.loadMore = loadMore;
  139. }
  140. setFirstLoadFinish(loadFinish) {
  141. this.loadFinish = loadFinish;
  142. }
  143. playRemoveItemAnimation(itemScript, animCallback) {
  144. this.dataList.splice(itemScript._itemId, 1);
  145. this.content.height = (this.itemHeight + this.spacing) * this.dataList.length; // get total content height
  146. let sequ = cc.sequence(cc.moveBy(0.2, -750, 0), animCallback);
  147. itemScript.node.runAction(sequ);
  148. }
  149. addItemsToDataList(model, head) {
  150. if (head) {
  151. this.dataList.unshift(model);
  152. } else {
  153. this.dataList.push(model);
  154. }
  155. this.actualCount = this.dataList.length < this.spawnCount ? this.dataList.length : this.spawnCount;
  156. //判断新的数据个数是否比设定的个数要大,不是的话就要创建新的item,是的话就直接复用已创建的
  157. if (this.dataList.length <= this.spawnCount) {
  158. let item = cc.instantiate(this.itemPrefab);
  159. this.content.height = (this.itemHeight + this.spacing) * this.dataList.length + this.topHeight;
  160. this.content.addChild(item);
  161. var i = 0;
  162. if (!head) {
  163. }
  164. item.setPosition(600, -(item.height * (0.5 + i) + this.spacing * (i + 1) + this.topHeight));
  165. let action = cc.moveBy(0.2,cc.v2(-600, 0));
  166. item.runAction(action);
  167. let itemScript = item.getComponent(this.scriptName);
  168. itemScript.setListViewAdapter(this);
  169. itemScript._itemId = 0;
  170. itemScript.updateItem(this.dataList[i], i, (newBuildingInfo, newItemId) => {
  171. this.dataList[newItemId] = newBuildingInfo;
  172. });
  173. this.scriptItems.unshift(itemScript);
  174. if (head) {
  175. for (let i = 1; i < this.actualCount; ++i) { // spawn items, we only need to do this once
  176. let itemScript = this.scriptItems[i];
  177. // this.scriptItems[i].node.y = -(this.itemHeight * (0.5 + i) + this.spacing * (i + 1) + this.topHeight);
  178. let moveY = -(this.itemHeight * (0.5 + i) + this.spacing * (i + 1) + this.topHeight);
  179. // let action = cc.moveTo(0.2,cc.v2(0, moveY));
  180. // itemScript.node.runAction(action);
  181. itemScript.node.y = moveY;
  182. itemScript._itemId = i;
  183. }
  184. }
  185. } else {
  186. this.content.height = (this.itemHeight + this.spacing) * this.dataList.length + this.topHeight;
  187. //已生成足够多的预制资源,从底部抽取来使用
  188. if (head) {
  189. let last = this.scriptItems.length - 1;
  190. let itemScript = this.scriptItems[last];
  191. var index = 0;
  192. itemScript.updateItem(this.dataList[index], index, (newBuildingInfo, newItemId) => {
  193. this.dataList[newItemId] = newBuildingInfo;
  194. });
  195. itemScript._itemId = 0;
  196. itemScript.node.y = -(this.itemHeight * (0.5 + index) + this.spacing * (index + 1) + this.topHeight);
  197. itemScript.node.x = 600;
  198. let action = cc.moveBy(0.2,cc.v2(-600, 0));
  199. itemScript.node.runAction(action);
  200. var temp = this.scriptItems[last];
  201. this.scriptItems.pop();
  202. this.scriptItems.unshift(temp);
  203. for (let i = 1; i < this.scriptItems.length; ++i) {
  204. let itemScript = this.scriptItems[i];
  205. let moveY = -(this.itemHeight * (0.5 + i) + this.spacing * (i + 1) + this.topHeight);
  206. // let action = cc.moveTo(0.2,cc.v2(0, moveY));
  207. // itemScript.node.runAction(action);
  208. itemScript.node.y = moveY;
  209. itemScript._itemId = i;
  210. }
  211. } else {
  212. }
  213. }
  214. }
  215. updateItemsAndDataList(dataList,itemScript) {
  216. this.dataList = dataList;
  217. let index = itemScript._itemId;
  218. let scriptItems = this.scriptItems;
  219. if (this.dataList.length >= scriptItems.length) {
  220. // itemScript.scheduleOnce(() => {
  221. // itemScript.node.x = 0;
  222. // }, 0.2);
  223. for (let i = 0; i < scriptItems.length; ++i) {
  224. let itemNode = scriptItems[i].node;
  225. let script = scriptItems[i];
  226. let itemId = script._itemId;
  227. if (itemId < this.dataList.length) {
  228. if (script._itemId > index) {
  229. let finish = cc.callFunc(() => {
  230. itemNode.y = itemNode.y - (this.itemHeight + this.spacing);
  231. // script.updateItem(this.dataList[itemId], itemId);
  232. script.updateItem(this.dataList[itemId], itemId, (newBuildingInfo, newItemId) => {
  233. this.dataList[newItemId] = newBuildingInfo;
  234. });
  235. }, this);
  236. let s = cc.sequence(cc.moveBy(0.2, 0, (this.itemHeight + this.spacing)), finish);
  237. itemNode.runAction(s);
  238. } else {
  239. // itemScript.scheduleOnce(() => {
  240. // script.updateItem(this.dataList[itemId], itemId);
  241. script.updateItem(this.dataList[itemId], itemId, (newBuildingInfo, newItemId) => {
  242. this.dataList[newItemId] = newBuildingInfo;
  243. });
  244. // }, 0.2);
  245. }
  246. }
  247. }
  248. } else {
  249. for (let i = 0; i < scriptItems.length; ++i) {
  250. let itemId = scriptItems[i]._itemId;
  251. if (itemId === itemScript._itemId) {
  252. this.scriptItems.splice(i, 1);
  253. }
  254. }
  255. for (let i = 0; i < scriptItems.length; ++i) {
  256. let itemNode = scriptItems[i].node;
  257. let script = scriptItems[i];
  258. if (script._itemId > index) {
  259. let newId = script._itemId - 1;
  260. itemNode.runAction(cc.moveBy(0.2, 0, (this.itemHeight + this.spacing)));
  261. // script.updateItem(this.dataList[newId], newId);
  262. script.updateItem(this.dataList[newId], newId, (newBuildingInfo, newItemId) => {
  263. this.dataList[newItemId] = newBuildingInfo;
  264. });
  265. }
  266. }
  267. itemScript.node.destroy();
  268. }
  269. }
  270. removeItem(itemScript) {
  271. let finish = cc.callFunc(() => {
  272. this.updateItemsAndDataList(itemScript);
  273. });
  274. this.playRemoveItemAnimation(itemScript, finish);
  275. }
  276. onDestroy() {
  277. this.scriptItems = [];
  278. this.content.removeAllChildren();
  279. }
  280. }
  281. module.exports = ListViewAdapter;