ListViewAdapter.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. for (let i = 0; i < this.scriptItems.length; ++i) {
  132. let itemScript = this.scriptItems[i];
  133. let itemId = itemScript._itemId;
  134. itemScript.updateItem(this.dataList[itemId], itemId, (newBuildingInfo, newItemId) => {
  135. this.dataList[newItemId] = newBuildingInfo;
  136. });
  137. }
  138. }
  139. addData(newdataList) {
  140. this.dataList = this.dataList.concat(newdataList);
  141. this.content.height = (this.itemHeight + this.spacing) * this.dataList.length; // get total content height
  142. }
  143. setLoadMoreCallBack(loadMore) {
  144. this.loadMore = loadMore;
  145. }
  146. setFirstLoadFinish(loadFinish) {
  147. this.loadFinish = loadFinish;
  148. }
  149. playRemoveItemAnimation(itemScript, animCallback) {
  150. this.dataList.splice(itemScript._itemId, 1);
  151. this.content.height = (this.itemHeight + this.spacing) * this.dataList.length; // get total content height
  152. let sequ = cc.sequence(cc.moveBy(0.2, -750, 0), animCallback);
  153. itemScript.node.runAction(sequ);
  154. }
  155. addItemsToDataList(model, head) {
  156. if (head) {
  157. this.dataList.unshift(model);
  158. } else {
  159. this.dataList.push(model);
  160. }
  161. this.actualCount = this.dataList.length < this.spawnCount ? this.dataList.length : this.spawnCount;
  162. //判断新的数据个数是否比设定的个数要大,不是的话就要创建新的item,是的话就直接复用已创建的
  163. if (this.dataList.length <= this.spawnCount) {
  164. let item = cc.instantiate(this.itemPrefab);
  165. this.content.height = (this.itemHeight + this.spacing) * this.dataList.length + this.topHeight;
  166. this.content.addChild(item);
  167. var i = 0;
  168. if (!head) {
  169. }
  170. item.setPosition(600, -(item.height * (0.5 + i) + this.spacing * (i + 1) + this.topHeight));
  171. let action = cc.moveBy(0.2,cc.v2(-600, 0));
  172. item.runAction(action);
  173. let itemScript = item.getComponent(this.scriptName);
  174. itemScript.setListViewAdapter(this);
  175. itemScript._itemId = 0;
  176. itemScript.updateItem(this.dataList[i], i, (newBuildingInfo, newItemId) => {
  177. this.dataList[newItemId] = newBuildingInfo;
  178. });
  179. this.scriptItems.unshift(itemScript);
  180. if (head) {
  181. for (let i = 1; i < this.actualCount; ++i) { // spawn items, we only need to do this once
  182. let itemScript = this.scriptItems[i];
  183. // this.scriptItems[i].node.y = -(this.itemHeight * (0.5 + i) + this.spacing * (i + 1) + this.topHeight);
  184. let moveY = -(this.itemHeight * (0.5 + i) + this.spacing * (i + 1) + this.topHeight);
  185. // let action = cc.moveTo(0.2,cc.v2(0, moveY));
  186. // itemScript.node.runAction(action);
  187. itemScript.node.y = moveY;
  188. itemScript._itemId = i;
  189. }
  190. }
  191. } else {
  192. this.content.height = (this.itemHeight + this.spacing) * this.dataList.length + this.topHeight;
  193. //已生成足够多的预制资源,从底部抽取来使用
  194. if (head) {
  195. let last = this.scriptItems.length - 1;
  196. let itemScript = this.scriptItems[last];
  197. var index = 0;
  198. itemScript.updateItem(this.dataList[index], index, (newBuildingInfo, newItemId) => {
  199. this.dataList[newItemId] = newBuildingInfo;
  200. });
  201. itemScript._itemId = 0;
  202. itemScript.node.y = -(this.itemHeight * (0.5 + index) + this.spacing * (index + 1) + this.topHeight);
  203. itemScript.node.x = 600;
  204. let action = cc.moveBy(0.2,cc.v2(-600, 0));
  205. itemScript.node.runAction(action);
  206. var temp = this.scriptItems[last];
  207. this.scriptItems.pop();
  208. this.scriptItems.unshift(temp);
  209. for (let i = 1; i < this.scriptItems.length; ++i) {
  210. let itemScript = this.scriptItems[i];
  211. let moveY = -(this.itemHeight * (0.5 + i) + this.spacing * (i + 1) + this.topHeight);
  212. // let action = cc.moveTo(0.2,cc.v2(0, moveY));
  213. // itemScript.node.runAction(action);
  214. itemScript.node.y = moveY;
  215. itemScript._itemId = i;
  216. }
  217. } else {
  218. }
  219. }
  220. }
  221. updateItemsAndDataList(dataList,itemScript) {
  222. this.dataList = dataList;
  223. let index = itemScript._itemId;
  224. let scriptItems = this.scriptItems;
  225. if (this.dataList.length >= scriptItems.length) {
  226. // itemScript.scheduleOnce(() => {
  227. // itemScript.node.x = 0;
  228. // }, 0.2);
  229. for (let i = 0; i < scriptItems.length; ++i) {
  230. let itemNode = scriptItems[i].node;
  231. let script = scriptItems[i];
  232. let itemId = script._itemId;
  233. if (itemId < this.dataList.length) {
  234. if (script._itemId > index) {
  235. let finish = cc.callFunc(() => {
  236. itemNode.y = itemNode.y - (this.itemHeight + this.spacing);
  237. // script.updateItem(this.dataList[itemId], itemId);
  238. script.updateItem(this.dataList[itemId], itemId, (newBuildingInfo, newItemId) => {
  239. this.dataList[newItemId] = newBuildingInfo;
  240. });
  241. }, this);
  242. let s = cc.sequence(cc.moveBy(0.2, 0, (this.itemHeight + this.spacing)), finish);
  243. itemNode.runAction(s);
  244. } else {
  245. // itemScript.scheduleOnce(() => {
  246. // script.updateItem(this.dataList[itemId], itemId);
  247. script.updateItem(this.dataList[itemId], itemId, (newBuildingInfo, newItemId) => {
  248. this.dataList[newItemId] = newBuildingInfo;
  249. });
  250. // }, 0.2);
  251. }
  252. }
  253. }
  254. } else {
  255. for (let i = 0; i < scriptItems.length; ++i) {
  256. let itemId = scriptItems[i]._itemId;
  257. if (itemId === itemScript._itemId) {
  258. this.scriptItems.splice(i, 1);
  259. }
  260. }
  261. for (let i = 0; i < scriptItems.length; ++i) {
  262. let itemNode = scriptItems[i].node;
  263. let script = scriptItems[i];
  264. if (script._itemId > index) {
  265. let newId = script._itemId - 1;
  266. itemNode.runAction(cc.moveBy(0.2, 0, (this.itemHeight + this.spacing)));
  267. // script.updateItem(this.dataList[newId], newId);
  268. script.updateItem(this.dataList[newId], newId, (newBuildingInfo, newItemId) => {
  269. this.dataList[newItemId] = newBuildingInfo;
  270. });
  271. }
  272. }
  273. itemScript.node.destroy();
  274. }
  275. }
  276. removeItem(itemScript) {
  277. let finish = cc.callFunc(() => {
  278. this.updateItemsAndDataList(itemScript);
  279. });
  280. this.playRemoveItemAnimation(itemScript, finish);
  281. }
  282. onDestroy() {
  283. this.scriptItems = [];
  284. this.content.removeAllChildren();
  285. }
  286. }
  287. module.exports = ListViewAdapter;