TableView.ts 28 KB

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