TableView.ts 18 KB

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