TableView.ts 29 KB

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