TableView.ts 18 KB

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