TableView.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. const { ccclass, property } = cc._decorator;
  2. /** 动态加载的滚动列表
  3. * - 滑动列表动态加载
  4. * - 支持多种类型预制体混合滚动
  5. * - item脚本需要有函数 setItemData 用于接收数据
  6. * - 多个prefab混合时,要为每条item指定 pfbType
  7. * - item、content锚点y需要为1其他为0.5
  8. * @author 薛鸿潇
  9. */
  10. @ccclass
  11. export default class TableView extends cc.Component {
  12. @property({ displayName: '预制条目', type: [cc.Prefab] })
  13. private pre_item: cc.Prefab[] = [];
  14. @property({ displayName: '脚本名', type: [cc.String] })
  15. private scr_item: string[] = [];
  16. /** 间距 */
  17. @property({ displayName: '间距' })
  18. private itemInterval: number = 0;
  19. /** 数据 */
  20. private itemData: any = null;
  21. /** 滚动列表组件 */
  22. private scrollView: cc.ScrollView = null;
  23. /** item预制体组 */
  24. private item: Array<any> = null;
  25. /** 滑动列表的内容节点 */
  26. private content: any = null;
  27. /** 列表高 */
  28. private layerHeight: any = null;
  29. /** ? */
  30. private firstItemIndex: number = 0;
  31. private lastItemIndex: number = 0;
  32. private firstItemData: any = {};
  33. private lastItemData: any = {};
  34. private itemArr: Array<any> = [];
  35. private itemNode: Array<any> = [];
  36. private itemPosMap: any = new Map();
  37. private initItemData: boolean = true;
  38. private count: number = 0;
  39. private itemCanMoveDown: boolean = null;
  40. private itemCanMoveUp: boolean = null;
  41. onLoad() {
  42. this.initPro();
  43. // 测试
  44. // let itemData = [];
  45. // for (let i = 0; i < 20; i++) {
  46. // let test_data = {
  47. // pfbType: 0,
  48. // }
  49. // itemData.push(test_data);
  50. // }
  51. // this.init(itemData);
  52. }
  53. private initPro() {
  54. this.scrollView = this.node.getComponent(cc.ScrollView);
  55. this.content = this.scrollView.content;
  56. this.item = this.pre_item;
  57. this.layerHeight = this.node.height;
  58. for (let i = 0; i < this.item.length; i++) {
  59. this.itemNode[i] = cc.instantiate(this.item[i]);
  60. }
  61. this.node.on('srollview-init', this.init, this);
  62. }
  63. /**
  64. * @param itemData 列表数据
  65. * @param isSkipInit 跳过初始化
  66. * @returns
  67. */
  68. public init(itemData: Array<any>, isSkipInit: boolean = false) {
  69. if (!itemData[0]) {
  70. return false;
  71. }
  72. this.clearItem();
  73. this.itemData = itemData; //item数据
  74. this.firstItemIndex = 0;
  75. this.lastItemIndex = 0;
  76. this.firstItemData = {};
  77. this.lastItemData = {};
  78. this.itemArr = [];
  79. this.itemPosMap = new Map();
  80. this.initItemData = true;
  81. this.count = 0;
  82. if (isSkipInit) return;
  83. // this.itemNode = [];
  84. // for (let i = 0; i < this.item.length; i++) {
  85. // this.itemNode[i] = cc.instantiate(this.item[i]);
  86. // }
  87. // cc.log(this.testTime(0));// 消耗计时
  88. this.initItem();
  89. this.getItemPos(0);
  90. this.scrollView.node.on('scrolling', this.callback, this);
  91. // cc.log('tableView结束:' + this.testTime());
  92. }
  93. /**
  94. * 刷新列表
  95. * 仅更新data数据
  96. */
  97. private updateList() {
  98. }
  99. private getItemPos(index: number) {
  100. let item_data_count = this.itemData.length;
  101. for (let i = index; i < item_data_count; i++) {
  102. let obj: any = {}
  103. let y;
  104. if (i === 0) {
  105. obj.startPos = 0;
  106. } else {
  107. obj.startPos = this.itemPosMap.get(i - 1).endPos;
  108. }
  109. let j = this.itemData[i].pfbType || 0;
  110. obj.endPos = obj.startPos + this.itemNode[j].height + this.itemInterval;
  111. this.itemPosMap.set(i, obj);
  112. }
  113. if (item_data_count > 0) {
  114. this.updateContentHeigh(this.itemPosMap.get(item_data_count - 1).endPos);
  115. }
  116. }
  117. /**
  118. * 实例化所有用到的item,控制实例化item的数目,暂定超出两个
  119. */
  120. private initItem() {
  121. let j = 0;
  122. for (let i = 0; i < this.itemData.length; i++) {
  123. if (this.content.height > this.layerHeight) {
  124. j++
  125. if (j > 2) {
  126. break;
  127. }
  128. }
  129. let y = 0;
  130. if (i > 0) {
  131. y = this.itemArr[i - 1].y - this.itemArr[i - 1].height - this.itemInterval;// 下一条目纵坐标
  132. }
  133. this.addItemNode(i, y);
  134. this.updateContentHeigh(this.itemArr[i].height - y);
  135. }
  136. }
  137. /**
  138. * 添加条目节点
  139. * @param i 编号
  140. * @param y 坐标y
  141. */
  142. private addItemNode(i: number, y: number) {
  143. let pfbType = this.itemData[i].pfbType || 0;
  144. let item = cc.instantiate(this.itemNode[pfbType]);
  145. item.parent = this.content;
  146. item.pfbType = pfbType;
  147. item.index = i;
  148. if (i === 0) {
  149. item.y = 0;
  150. } else {
  151. item.y = y;
  152. }
  153. item.x = 0;
  154. //对item赋值
  155. let comp_script = item.getComponent(this.scr_item[pfbType]);
  156. comp_script && comp_script.setItemData(this.itemData[i], this);
  157. this.itemArr.push(item);
  158. // cc.log('生成itemNode' + i);
  159. }
  160. /**
  161. * 更新centent高
  162. * @param num
  163. */
  164. private updateContentHeigh(num: number) {
  165. this.content.height = num > this.layerHeight ? num : this.layerHeight;
  166. // cc.log('滚动条高度:', this.content.height);
  167. }
  168. /**
  169. * 触摸滚动条的函数回调
  170. * @param event
  171. * @param eventType
  172. * @returns
  173. */
  174. private callback(event, eventType) {
  175. // cc.log(event && event.type || eventType)
  176. if (this.content.height > this.layerHeight) {
  177. let firstItemPos = this.scrollView.getScrollOffset().y;
  178. let lastItemPos = firstItemPos + this.layerHeight;
  179. if (firstItemPos < 0) return;
  180. if (this.initItemData) {
  181. // cc.log('111:%o', this.itemPosMap)
  182. this.initItemData = false;
  183. this.updateFirstItemIndex();
  184. this.itemCanMoveDown = true;
  185. this.updateLastItemIndex();
  186. this.itemCanMoveDown = false;
  187. }
  188. //超出边界直接返回.
  189. //滚动条向上滑动可能会触发的函数
  190. if (firstItemPos > this.firstItemData.endPos) {
  191. if (this.lastItemIndex + 1 < this.itemData.length) {
  192. this.updateFirstItemIndex();
  193. }
  194. this.count++;
  195. }
  196. if (lastItemPos > this.lastItemData.endPos) {
  197. if (this.lastItemIndex + 1 < this.itemData.length) {
  198. this.itemCanMoveDown = true;
  199. this.updateLastItemIndex();
  200. this.itemCanMoveDown = false;
  201. }
  202. }
  203. //滚动条向下滑动可能会触发的函数
  204. if (lastItemPos < this.lastItemData.startPos) {
  205. this.updateLastItemIndex();
  206. this.count--;
  207. }
  208. if (firstItemPos < this.firstItemData.startPos) {
  209. this.itemCanMoveUp = true;
  210. this.updateFirstItemIndex();
  211. this.itemCanMoveUp = false;
  212. }
  213. }
  214. }
  215. private updateFirstItemIndex() {
  216. let num = this.firstItemIndex;
  217. if (this.itemCanMoveUp && num > this.getItemIndex()[0] && num > 0) {
  218. this.itemMoveUp(this.firstItemIndex - 1);
  219. }
  220. this.updateItemIndex();
  221. }
  222. private updateLastItemIndex() {
  223. let num = this.lastItemIndex;
  224. if (this.itemCanMoveDown && num < this.getItemIndex()[1] && num + 1 < this.itemData.length) {
  225. this.itemMoveDown(this.lastItemIndex + 1);
  226. }
  227. this.updateItemIndex();
  228. }
  229. private updateItemIndex() {
  230. //cc.log(this.firstItemIndex, this.lastItemIndex, this.itemArr, this.itemData)
  231. }
  232. /**
  233. * 得到滚动条此时状态下应有的itemNode元素,包括滚动条上方一个,滚动条下方一个
  234. * @returns
  235. */
  236. private getItemIndex() {
  237. let firstItemPos = this.scrollView.getScrollOffset().y;
  238. let lastItemPos = firstItemPos + this.layerHeight;
  239. let arr = [];
  240. this.itemPosMap.forEach((value, key) => {
  241. let status1 = value.startPos <= firstItemPos && value.endPos > firstItemPos;
  242. let status2 = value.startPos >= firstItemPos && value.endPos < lastItemPos;
  243. let status3 = value.startPos <= lastItemPos && value.endPos > lastItemPos;
  244. if (status1) {
  245. this.firstItemData.startPos = value.startPos;
  246. this.firstItemData.endPos = value.endPos;
  247. this.firstItemIndex = key;
  248. arr.push(key);
  249. }
  250. if (status3) {
  251. this.lastItemData.startPos = value.startPos;
  252. this.lastItemData.endPos = value.endPos;
  253. this.lastItemIndex = key;
  254. arr.push(key);
  255. }
  256. })
  257. return arr;
  258. }
  259. /**
  260. * 滚动到顶部【滚动条顺序是从上到下开始遍历】
  261. * @param num
  262. * @returns
  263. */
  264. private itemMoveUp(num: number) {
  265. if (num < 0 || this.lastItemIndex + 1 < num || num + 1 > this.itemData.length) {
  266. return;
  267. }
  268. if (!this.hasItem(num)) {
  269. this.itemMove(num, -this.itemPosMap.get(num).startPos);
  270. }
  271. num++;
  272. return this.itemMoveUp(num);
  273. }
  274. /** 滚动到底部 */
  275. private itemMoveDown(num: number) {
  276. if (num < 0 || this.firstItemIndex - 1 > num || num + 1 > this.itemData.length) {
  277. return;
  278. }
  279. if (!this.hasItem(num)) {
  280. this.itemMove(num, -this.itemPosMap.get(num).startPos);
  281. }
  282. num--;
  283. return this.itemMoveDown(num);
  284. }
  285. /**
  286. * 判断指定index位置是否存在itemNode
  287. * @param index 节点下标
  288. * @returns
  289. */
  290. private hasItem(index: number) {
  291. for (let i = 0; i < this.itemArr.length; i++) {
  292. if (this.itemArr[i].index === index) {
  293. return true;
  294. }
  295. }
  296. return false;
  297. }
  298. /**
  299. * 移动条目
  300. * 逻辑判断,第一种情况,修改itemArr数组的某个对象,第二种情况实例化一个新itemNode
  301. * @param index 条目节点标记
  302. * @param y
  303. * @returns
  304. */
  305. private itemMove(index: number, y: number) {
  306. for (let i = 0; i < this.itemArr.length; i++) {
  307. //index存在-1的情况,类似于在缓存池里的item.
  308. let status1 = this.itemArr[i].index < this.firstItemIndex - 1 ? true : false;
  309. let status2 = this.itemArr[i].index > this.lastItemIndex + 1 ? true : false;
  310. let status3 = this.itemArr[i].pfbType === this.itemData[index].pfbType;
  311. //cc.log('item的索引', this.firstItemIndex, this.lastItemIndex)
  312. if (status1 && status3 || status2 && status3) {
  313. //cc.log(i, index, this.itemArr, this.content.height);
  314. //给item赋值还有设置位置
  315. this.itemArr[i].index = index;
  316. this.itemArr[i].y = y;
  317. let comp_script = this.itemArr[i].getComponent(this.scr_item[this.itemArr[i].pfbType]);
  318. comp_script && comp_script.setItemData(this.itemData[index], this);
  319. return;
  320. }
  321. }
  322. this.addItemNode(index, y);
  323. }
  324. /**
  325. * 得到相关位置的排序index
  326. * 方法待验证,可能有问题
  327. * @param pos 坐标
  328. * @returns
  329. */
  330. public getPosIndex(pos: cc.Vec2) {
  331. for (let [key, value] of this.itemPosMap.entries()) {
  332. if (value.endPos > pos && value.startPos <= pos) {
  333. return key;
  334. }
  335. }
  336. }
  337. /**
  338. * 添加条目
  339. * @param obj 数据对象
  340. * @returns
  341. */
  342. public addItem(obj: any) {
  343. this.itemData.push(obj);
  344. this.getItemPos(this.itemData.length - 1);
  345. let endPos = this.itemPosMap.get(this.itemData.length - 1).endPos;
  346. if (endPos - this.layerHeight > 0) {
  347. let startPos = endPos - this.layerHeight;
  348. //得到当前的firstItemIndex;
  349. for (let i = this.itemData.length - 1; i >= 0; i--) {
  350. if (this.itemPosMap.get(i).endPos > startPos && this.itemPosMap.get(i).startPos <= startPos) {
  351. this.firstItemIndex = i;
  352. }
  353. }
  354. this.scrollView.scrollToBottom();
  355. this.lastItemIndex = this.itemData.length - 1;
  356. let num = this.firstItemIndex - 1 > 0 ? (this.firstItemIndex - 1) : 0;
  357. this.itemMoveUp(num);
  358. return true;
  359. } else {
  360. this.firstItemIndex = 0;
  361. this.lastItemIndex = this.itemData.length - 1;
  362. this.itemMoveUp(this.firstItemIndex);
  363. return false;
  364. }
  365. }
  366. /**
  367. * 清理条目
  368. */
  369. public clearItem() {
  370. this.itemData = [];
  371. this.itemPosMap.clear();
  372. this.scrollView.scrollToTop();
  373. this.content.height = 0;
  374. for (let i in this.itemArr) {
  375. // this.itemArr[i].index = -1;
  376. // this.itemArr[i].y = 3000;
  377. this.itemArr[i].destroy();
  378. }
  379. }
  380. /**
  381. * 删除指定条目
  382. * @param i 节点.index
  383. * @returns
  384. */
  385. public deleteItem(i: number) {
  386. this.itemData.splice(i, 1);
  387. const item_data_count = this.itemData.length;
  388. if (item_data_count <= 0) return;
  389. this.getItemPos(item_data_count - 1);
  390. //改变this.itemArr的内容
  391. for (let j = 0; j < this.itemArr.length; j++) {
  392. if (this.itemArr[j].index === i) {
  393. this.itemArr[j].index = -1;
  394. this.itemArr[j].y = 3000;
  395. }
  396. if (this.itemArr[j].index > i) {
  397. let num = this.itemArr[j].index;
  398. this.itemArr[j].y = -this.itemPosMap.get(num - 1).startPos;
  399. this.itemArr[j].index = num - 1;
  400. }
  401. }
  402. this.updateContentHeigh(this.itemPosMap.get(this.itemData.length - 1).endPos);
  403. if (!this.lastItemIndex) this.getItemIndex();// 初始化时没有初始化此值,会导致为0.不能创建新的item,此处校验
  404. this.itemMoveUp(this.firstItemIndex);//
  405. }
  406. /**
  407. * 重置指定item
  408. * @param index 节点下标
  409. * @param item_data 新的数据
  410. */
  411. public resetItemData(index: number, item_data?: any) {
  412. for (let i = 0; i < this.itemArr.length; i++) {
  413. if (this.itemArr[i].index === index) {
  414. if (item_data) this.itemData[i] = item_data;
  415. let comp_script = this.itemArr[i].getComponent(this.scr_item[this.itemArr[i].pfbType]);
  416. comp_script && comp_script.setItemData(this.itemData[index], this);
  417. break;
  418. }
  419. }
  420. }
  421. private lastResetItemInfoHeight: any = null;
  422. private lastResetItemIndex: any = null;
  423. /**
  424. * 重置条目大小
  425. * @param index 节点下标
  426. * @param infoHeight 新的高度
  427. */
  428. public resetItemSize(index: number, infoHeight: number) {
  429. let func = (function (index, infoHeight) {
  430. for (let i = 0; i < this.itemArr.length; i++) {
  431. if (this.itemArr[i].index > index) {
  432. this.itemArr[i].y -= infoHeight;
  433. }
  434. }
  435. for (let [key, value] of this.itemPosMap.entries()) {
  436. if (key === index) {
  437. value.endPos += infoHeight;
  438. }
  439. if (key > index) {
  440. value.endPos += infoHeight;
  441. value.startPos += infoHeight;
  442. }
  443. }
  444. this.lastResetItemInfoHeight = infoHeight;
  445. this.lastResetItemIndex = index;
  446. }).bind(this);
  447. if (this.lastResetItemIndex !== null && this.lastResetItemInfoHeight) {
  448. if (this.lastResetItemIndex === index) {
  449. func(this.lastResetItemIndex, -this.lastResetItemInfoHeight);
  450. this.lastResetItemIndex = null;
  451. this.lastResetItemInfoHeight = 0;
  452. } else {
  453. func(this.lastResetItemIndex, -this.lastResetItemInfoHeight);
  454. func(index, infoHeight);
  455. }
  456. } else {
  457. func(index, infoHeight);
  458. }
  459. this.itemMoveUp(this.firstItemIndex);
  460. this.updateContentHeigh(this.itemPosMap.get(this.itemData.length - 1).endPos);
  461. }
  462. }