TableView.ts 29 KB

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