TableView.ts 30 KB

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