TableView.ts 25 KB

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