TableView.ts 29 KB

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