TableView.ts 19 KB

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