1
0

ListViewAdapter.js 13 KB

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