TableView.ts 17 KB

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