List.ts 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143
  1. /*列表组件*/
  2. const { ccclass, property, disallowMultiple, menu, executionOrder, requireComponent } = _decorator;
  3. import { Node, Component, Enum, tween, _decorator, EventHandler, Tween, ScrollView, Prefab, Layout, Vec2, Size, isValid, Vec3, Widget, UITransform, CCFloat, CCBoolean, CCInteger } from 'cc';
  4. import { DEV } from 'cc/env';
  5. import ListItem from './ListItem';
  6. import { PoolManager } from '../core/manager/PoolManager';
  7. enum TemplateType {
  8. NODE = 1,
  9. PREFAB = 2,
  10. }
  11. enum SlideType {
  12. NORMAL = 1,//普通
  13. ADHERING = 2,//粘附模式,将强制关闭滚动惯性
  14. PAGE = 3,//页面模式,将强制关闭滚动惯性
  15. }
  16. enum SelectedType {
  17. NONE = 0,
  18. SINGLE = 1,//单选
  19. MULT = 2,//多选
  20. }
  21. @ccclass
  22. @disallowMultiple()
  23. @menu('List')
  24. @requireComponent(ScrollView)
  25. //脚本生命周期回调的执行优先级。小于 0 的脚本将优先执行,大于 0 的脚本将最后执行。该优先级只对 onLoad, onEnable, start, update 和 lateUpdate 有效,对 onDisable 和 onDestroy 无效。
  26. @executionOrder(-5000)
  27. export default class List extends Component {
  28. //模板类型
  29. @property({ type: Enum(TemplateType), tooltip: DEV && '模板类型', })
  30. private templateType: TemplateType = TemplateType.NODE;
  31. //模板Item(Node)
  32. @property({
  33. type: Node,
  34. tooltip: DEV && '模板Item',
  35. visible() { return this.templateType == TemplateType.NODE; }
  36. })
  37. tmpNode: Node = null;
  38. //模板Item(Prefab)
  39. @property({
  40. type: Prefab,
  41. tooltip: DEV && '模板Item',
  42. visible() { return this.templateType == TemplateType.PREFAB; }
  43. })
  44. tmpPrefab: Prefab = null;
  45. //滑动模式
  46. @property({})
  47. private _slideMode: SlideType = SlideType.NORMAL;
  48. @property({
  49. type: Enum(SlideType),
  50. tooltip: DEV && '滑动模式'
  51. })
  52. set slideMode(val: SlideType) {
  53. this._slideMode = val;
  54. }
  55. get slideMode() {
  56. return this._slideMode;
  57. }
  58. //翻页作用距离
  59. @property({
  60. type: CCFloat,
  61. range: [0, 1, .1],
  62. tooltip: DEV && '翻页作用距离',
  63. slide: true,
  64. visible() { return this._slideMode == SlideType.PAGE; }
  65. })
  66. public pageDistance: number = .3;
  67. //页面改变事件
  68. @property({
  69. type: EventHandler,
  70. tooltip: DEV && '页面改变事件',
  71. visible() { return this._slideMode == SlideType.PAGE; }
  72. })
  73. private pageChangeEvent: EventHandler = new EventHandler();
  74. //是否为虚拟列表(动态列表)
  75. @property({})
  76. private _virtual: boolean = true;
  77. @property({
  78. type: CCBoolean,
  79. tooltip: DEV && '是否为虚拟列表(动态列表)'
  80. })
  81. set virtual(val: boolean) {
  82. if (val != null)
  83. this._virtual = val;
  84. if (!DEV && this._numItems != 0) {
  85. this._onScrolling();
  86. }
  87. }
  88. get virtual() {
  89. return this._virtual;
  90. }
  91. //是否为循环列表
  92. @property({
  93. tooltip: DEV && '是否为循环列表',
  94. visible() {
  95. let val: boolean = /*this.virtual &&*/ this.slideMode == SlideType.NORMAL;
  96. if (!val)
  97. this.cyclic = false;
  98. return val;
  99. }
  100. })
  101. public cyclic: boolean = false;
  102. //缺省居中
  103. @property({
  104. tooltip: DEV && 'Item数量不足以填满Content时,是否居中显示Item(不支持Grid布局)',
  105. visible() { return this.virtual; }
  106. })
  107. public lackCenter: boolean = false;
  108. //缺省可滑动
  109. @property({
  110. tooltip: DEV && 'Item数量不足以填满Content时,是否可滑动',
  111. visible() {
  112. let val: boolean = this.virtual && !this.lackCenter;
  113. if (!val)
  114. this.lackSlide = false;
  115. return val;
  116. }
  117. })
  118. public lackSlide: boolean = false;
  119. //刷新频率
  120. @property({ type: CCInteger })
  121. private _updateRate: number = 0;
  122. @property({
  123. type: CCInteger,
  124. range: [0, 6, 1],
  125. tooltip: DEV && '刷新频率(值越大刷新频率越低、性能越高)',
  126. slide: true,
  127. })
  128. set updateRate(val: number) {
  129. if (val >= 0 && val <= 6) {
  130. this._updateRate = val;
  131. }
  132. }
  133. get updateRate() {
  134. return this._updateRate;
  135. }
  136. //分帧渲染(每帧渲染的Item数量(<=0时关闭分帧渲染))
  137. @property({
  138. type: CCInteger,
  139. range: [0, 12, 1],
  140. tooltip: DEV && '逐帧渲染时,每帧渲染的Item数量(<=0时关闭分帧渲染)',
  141. slide: true,
  142. })
  143. public frameByFrameRenderNum: number = 0;
  144. //渲染事件(渲染器)
  145. @property({
  146. type: EventHandler,
  147. tooltip: DEV && '渲染事件(渲染器)',
  148. })
  149. public renderEvent: EventHandler = new EventHandler();
  150. //选择模式
  151. @property({
  152. type: Enum(SelectedType),
  153. tooltip: DEV && '选择模式'
  154. })
  155. public selectedMode: SelectedType = SelectedType.NONE;
  156. //触发选择事件
  157. @property({
  158. type: EventHandler,
  159. tooltip: DEV && '触发选择事件',
  160. visible() { return this.selectedMode > SelectedType.NONE; }
  161. })
  162. public selectedEvent: EventHandler = new EventHandler();
  163. @property({
  164. tooltip: DEV && '是否重复响应单选事件',
  165. visible() { return this.selectedMode == SelectedType.SINGLE; }
  166. })
  167. public repeatEventSingle: boolean = false;
  168. //当前选择id
  169. private _selectedId: number = -1;
  170. private _lastSelectedId: number;
  171. private multSelected: number[];
  172. set selectedId(val: number) {
  173. let t: any = this;
  174. let item: any;
  175. switch (t.selectedMode) {
  176. case SelectedType.SINGLE: {
  177. if (!t.repeatEventSingle && val == t._selectedId)
  178. return;
  179. item = t.getItemByListId(val);
  180. // if (!item && val >= 0)
  181. // return;
  182. let listItem: ListItem;
  183. if (t._selectedId >= 0)
  184. t._lastSelectedId = t._selectedId;
  185. else //如果<0则取消选择,把_lastSelectedId也置空吧,如果以后有特殊需求再改吧。
  186. t._lastSelectedId = null;
  187. t._selectedId = val;
  188. if (item) {
  189. listItem = item.getComponent(ListItem);
  190. listItem.selected = true;
  191. }
  192. if (t._lastSelectedId >= 0 && t._lastSelectedId != t._selectedId) {
  193. let lastItem: any = t.getItemByListId(t._lastSelectedId);
  194. if (lastItem) {
  195. lastItem.getComponent(ListItem).selected = false;
  196. }
  197. }
  198. if (t.selectedEvent) {
  199. EventHandler.emitEvents([t.selectedEvent], item, val % this._actualNumItems, t._lastSelectedId == null ? null : (t._lastSelectedId % this._actualNumItems));
  200. }
  201. break;
  202. }
  203. case SelectedType.MULT: {
  204. item = t.getItemByListId(val);
  205. if (!item)
  206. return;
  207. let listItem = item.getComponent(ListItem);
  208. if (t._selectedId >= 0)
  209. t._lastSelectedId = t._selectedId;
  210. t._selectedId = val;
  211. let bool: boolean = !listItem.selected;
  212. listItem.selected = bool;
  213. let sub: number = t.multSelected.indexOf(val);
  214. if (bool && sub < 0) {
  215. t.multSelected.push(val);
  216. } else if (!bool && sub >= 0) {
  217. t.multSelected.splice(sub, 1);
  218. }
  219. if (t.selectedEvent) {
  220. EventHandler.emitEvents([t.selectedEvent], item, val % this._actualNumItems, t._lastSelectedId == null ? null : (t._lastSelectedId % this._actualNumItems), bool);
  221. }
  222. break;
  223. }
  224. }
  225. }
  226. get selectedId() {
  227. return this._selectedId;
  228. }
  229. private _forceUpdate: boolean = false;
  230. private _align: number;
  231. private _horizontalDir: number;
  232. private _verticalDir: number;
  233. private _startAxis: number;
  234. private _alignCalcType: number;
  235. public content: Node;
  236. private _contentUt: UITransform;
  237. private firstListId: number;
  238. public displayItemNum: number;
  239. private _updateDone: boolean = true;
  240. private _updateCounter: number;
  241. public _actualNumItems: number;
  242. private _cyclicNum: number;
  243. private _cyclicPos1: number;
  244. private _cyclicPos2: number;
  245. //列表数量
  246. @property({
  247. serializable: false
  248. })
  249. private _numItems: number = 0;
  250. set numItems(val: number) {
  251. let t = this;
  252. //t.recycle();
  253. if (!t.checkInited(false))
  254. return;
  255. if (val == null || val < 0) {
  256. console.error('numItems set the wrong::', val);
  257. return;
  258. }
  259. t._actualNumItems = t._numItems = val;
  260. t._forceUpdate = true;
  261. if (t._virtual) {
  262. t._resizeContent();
  263. if (t.cyclic) {
  264. t._numItems = t._cyclicNum * t._numItems;
  265. }
  266. t._onScrolling();
  267. if (!t.frameByFrameRenderNum && t.slideMode == SlideType.PAGE)
  268. t.curPageNum = t.nearestListId;
  269. } else {
  270. if (t.cyclic) {
  271. t._resizeContent();
  272. t._numItems = t._cyclicNum * t._numItems;
  273. }
  274. let layout: Layout = t.content.getComponent(Layout);
  275. if (layout) {
  276. layout.enabled = true;
  277. }
  278. t._delRedundantItem();
  279. t.firstListId = 0;
  280. if (t.frameByFrameRenderNum > 0) {
  281. //先渲染几个出来
  282. let len: number = t.frameByFrameRenderNum > t._numItems ? t._numItems : t.frameByFrameRenderNum;
  283. for (let n: number = 0; n < len; n++) {
  284. t._createOrUpdateItem2(n);
  285. }
  286. if (t.frameByFrameRenderNum < t._numItems) {
  287. t._updateCounter = t.frameByFrameRenderNum;
  288. t._updateDone = false;
  289. }
  290. } else {
  291. for (let n: number = 0; n < t._numItems; n++) {
  292. t._createOrUpdateItem2(n);
  293. }
  294. t.displayItemNum = t._numItems;
  295. }
  296. }
  297. }
  298. get numItems() {
  299. return this._actualNumItems;
  300. }
  301. private _inited: boolean = false;
  302. private _scrollView: ScrollView;
  303. get scrollView() {
  304. return this._scrollView;
  305. }
  306. private _layout: Layout;
  307. private _resizeMode: number;
  308. private _topGap: number;
  309. private _rightGap: number;
  310. private _bottomGap: number;
  311. private _leftGap: number;
  312. private _columnGap: number;
  313. private _lineGap: number;
  314. private _colLineNum: number;
  315. private _lastDisplayData: number[];
  316. public displayData: any[];
  317. private _itemTmp: any;
  318. private _itemTmpUt: UITransform;
  319. private _needUpdateWidget: boolean = false;
  320. private _itemSize: Size;
  321. private _sizeType: boolean;
  322. private _oScale: Vec3;
  323. public _customSize: any;
  324. private frameCount: number;
  325. private _aniDelRuning: boolean = false;
  326. private _aniDelCB: Function;
  327. private _aniDelItem: any;
  328. private _aniDelBeforePos: Vec2;
  329. private _aniDelBeforeScale: number;
  330. private viewTop: number;
  331. private viewRight: number;
  332. private viewBottom: number;
  333. private viewLeft: number;
  334. private _doneAfterUpdate: boolean = false;
  335. private elasticTop: number;
  336. private elasticRight: number;
  337. private elasticBottom: number;
  338. private elasticLeft: number;
  339. private scrollToListId: number;
  340. private adhering: boolean = false;
  341. private _adheringBarrier: boolean = false;
  342. private nearestListId: number;
  343. public curPageNum: number = 0;
  344. private _beganPos: number;
  345. private _scrollPos: number;
  346. private _curScrollIsTouch: boolean;//当前滑动是否为手动
  347. private _scrollToListId: number;
  348. private _scrollToEndTime: number;
  349. private _scrollToSo: any;
  350. private _lack: boolean;
  351. private _allItemSize: number;
  352. private _allItemSizeNoEdge: number;
  353. private _scrollItem: any;//当前控制 ScrollView 滚动的 Item
  354. private _thisNodeUt: UITransform;
  355. onLoad() {
  356. this._init();
  357. }
  358. public recycle(){
  359. this.content.children.forEach((e: any) => {
  360. if(e != this.tmpNode && isValid(e)){
  361. e.scale = this._oScale;
  362. PoolManager.putNode(e);
  363. }
  364. });
  365. }
  366. onDestroy() {
  367. let t: any = this;
  368. if (isValid(t._itemTmp))
  369. t._itemTmp.destroy();
  370. if (isValid(t.tmpNode))
  371. t.tmpNode.destroy();
  372. }
  373. onDisable() {
  374. // if (!EDITOR)
  375. this._unregisterEvent();
  376. this.recycle();
  377. }
  378. onEnable() {
  379. // if (!EDITOR)
  380. this._registerEvent();
  381. this._init();
  382. // 处理重新显示后,有可能上一次的动画移除还未播放完毕,导致动画卡住的问题
  383. if (this._aniDelRuning) {
  384. this._aniDelRuning = false;
  385. if (this._aniDelItem) {
  386. if (this._aniDelBeforePos) {
  387. this._aniDelItem.position = this._aniDelBeforePos;
  388. delete this._aniDelBeforePos;
  389. }
  390. if (this._aniDelBeforeScale) {
  391. this._aniDelItem.scale = this._aniDelBeforeScale;
  392. delete this._aniDelBeforeScale;
  393. }
  394. delete this._aniDelItem;
  395. }
  396. if (this._aniDelCB) {
  397. this._aniDelCB();
  398. delete this._aniDelCB;
  399. }
  400. }
  401. }
  402. //注册事件
  403. _registerEvent() {
  404. let t: any = this;
  405. t.node.on(Node.EventType.TOUCH_START, t._onTouchStart, t);
  406. t.node.on('touch-up', t._onTouchUp, t);
  407. t.node.on(Node.EventType.TOUCH_CANCEL, t._onTouchCancelled, t);
  408. t.node.on('scroll-began', t._onScrollBegan, t);
  409. t.node.on('scroll-ended', t._onScrollEnded, t);
  410. t.node.on('scrolling', t._onScrolling, t);
  411. t.node.on(Node.EventType.SIZE_CHANGED, t._onSizeChanged, t);
  412. }
  413. //卸载事件
  414. _unregisterEvent() {
  415. let t: any = this;
  416. t.node.off(Node.EventType.TOUCH_START, t._onTouchStart, t);
  417. t.node.off('touch-up', t._onTouchUp, t);
  418. t.node.off(Node.EventType.TOUCH_CANCEL, t._onTouchCancelled, t);
  419. t.node.off('scroll-began', t._onScrollBegan, t);
  420. t.node.off('scroll-ended', t._onScrollEnded, t);
  421. t.node.off('scrolling', t._onScrolling, t);
  422. t.node.off(Node.EventType.SIZE_CHANGED, t._onSizeChanged, t);
  423. }
  424. //初始化各种..
  425. _init() {
  426. let t: any = this;
  427. if (t._inited)
  428. return;
  429. t._thisNodeUt = t.node.getComponent(UITransform);
  430. t._scrollView = t.node.getComponent(ScrollView);
  431. t.content = t._scrollView.content;
  432. t._contentUt = t.content.getComponent(UITransform);
  433. if (!t.content) {
  434. console.error(t.node.name + "'s ScrollView unset content!");
  435. return;
  436. }
  437. t._layout = t.content.getComponent(Layout);
  438. t._align = t._layout.type; //排列模式
  439. t._resizeMode = t._layout.resizeMode; //自适应模式
  440. t._startAxis = t._layout.startAxis;
  441. t._topGap = t._layout.paddingTop; //顶边距
  442. t._rightGap = t._layout.paddingRight; //右边距
  443. t._bottomGap = t._layout.paddingBottom; //底边距
  444. t._leftGap = t._layout.paddingLeft; //左边距
  445. t._columnGap = t._layout.spacingX; //列距
  446. t._lineGap = t._layout.spacingY; //行距
  447. t._colLineNum; //列数或行数(非GRID模式则=1,表示单列或单行);
  448. t._verticalDir = t._layout.verticalDirection; //垂直排列子节点的方向
  449. t._horizontalDir = t._layout.horizontalDirection; //水平排列子节点的方向
  450. let pNode: Node = t.templateType == TemplateType.PREFAB ? t.tmpPrefab : t.tmpNode;
  451. t.setTemplateItem(PoolManager.getNode(pNode));
  452. // 特定的滑动模式处理
  453. if (t._slideMode == SlideType.ADHERING || t._slideMode == SlideType.PAGE) {
  454. t._scrollView.inertia = false;
  455. t._scrollView._onMouseWheel = function () {
  456. return;
  457. };
  458. }
  459. if (!t.virtual) // lackCenter 仅支持 Virtual 模式
  460. t.lackCenter = false;
  461. t._lastDisplayData = []; //最后一次刷新的数据
  462. t.displayData = []; //当前数据
  463. t._forceUpdate = false; //是否强制更新
  464. t._updateCounter = 0; //当前分帧渲染帧数
  465. t._updateDone = true; //分帧渲染是否完成
  466. t.curPageNum = 0; //当前页数
  467. if (t.cyclic || 0) {
  468. t._scrollView._processAutoScrolling = this._processAutoScrolling.bind(t);
  469. t._scrollView._startBounceBackIfNeeded = function () {
  470. return false;
  471. }
  472. }
  473. switch (t._align) {
  474. case Layout.Type.HORIZONTAL: {
  475. switch (t._horizontalDir) {
  476. case Layout.HorizontalDirection.LEFT_TO_RIGHT:
  477. t._alignCalcType = 1;
  478. break;
  479. case Layout.HorizontalDirection.RIGHT_TO_LEFT:
  480. t._alignCalcType = 2;
  481. break;
  482. }
  483. break;
  484. }
  485. case Layout.Type.VERTICAL: {
  486. switch (t._verticalDir) {
  487. case Layout.VerticalDirection.TOP_TO_BOTTOM:
  488. t._alignCalcType = 3;
  489. break;
  490. case Layout.VerticalDirection.BOTTOM_TO_TOP:
  491. t._alignCalcType = 4;
  492. break;
  493. }
  494. break;
  495. }
  496. case Layout.Type.GRID: {
  497. switch (t._startAxis) {
  498. case Layout.AxisDirection.HORIZONTAL:
  499. switch (t._verticalDir) {
  500. case Layout.VerticalDirection.TOP_TO_BOTTOM:
  501. t._alignCalcType = 3;
  502. break;
  503. case Layout.VerticalDirection.BOTTOM_TO_TOP:
  504. t._alignCalcType = 4;
  505. break;
  506. }
  507. break;
  508. case Layout.AxisDirection.VERTICAL:
  509. switch (t._horizontalDir) {
  510. case Layout.HorizontalDirection.LEFT_TO_RIGHT:
  511. t._alignCalcType = 1;
  512. break;
  513. case Layout.HorizontalDirection.RIGHT_TO_LEFT:
  514. t._alignCalcType = 2;
  515. break;
  516. }
  517. break;
  518. }
  519. break;
  520. }
  521. }
  522. // 清空 content
  523. // t.content.children.forEach((child: Node) => {
  524. // child.removeFromParent();
  525. // if (child != t.tmpNode && child.isValid)
  526. // child.destroy();
  527. // });
  528. t.content.removeAllChildren();
  529. t._inited = true;
  530. }
  531. /**
  532. * 为了实现循环列表,必须覆写cc.ScrollView的某些函数
  533. * @param {Number} dt
  534. */
  535. _processAutoScrolling(dt: number) {
  536. // ------------- scroll-view 里定义的一些常量 -------------
  537. const OUT_OF_BOUNDARY_BREAKING_FACTOR = 0.05;
  538. const EPSILON = 1e-4;
  539. const ZERO = new Vec3();
  540. const quintEaseOut = (time: number) => {
  541. time -= 1;
  542. return (time * time * time * time * time + 1);
  543. };
  544. // ------------- scroll-view 里定义的一些常量 -------------
  545. let sv: ScrollView = this._scrollView;
  546. const isAutoScrollBrake = sv['_isNecessaryAutoScrollBrake']();
  547. const brakingFactor = isAutoScrollBrake ? OUT_OF_BOUNDARY_BREAKING_FACTOR : 1;
  548. sv['_autoScrollAccumulatedTime'] += dt * (1 / brakingFactor);
  549. let percentage = Math.min(1, sv['_autoScrollAccumulatedTime'] / sv['_autoScrollTotalTime']);
  550. if (sv['_autoScrollAttenuate']) {
  551. percentage = quintEaseOut(percentage);
  552. }
  553. const clonedAutoScrollTargetDelta = sv['_autoScrollTargetDelta'].clone();
  554. clonedAutoScrollTargetDelta.multiplyScalar(percentage);
  555. const clonedAutoScrollStartPosition = sv['_autoScrollStartPosition'].clone();
  556. clonedAutoScrollStartPosition.add(clonedAutoScrollTargetDelta);
  557. let reachedEnd = Math.abs(percentage - 1) <= EPSILON;
  558. const fireEvent = Math.abs(percentage - 1) <= sv['getScrollEndedEventTiming']();
  559. if (fireEvent && !sv['_isScrollEndedWithThresholdEventFired']) {
  560. sv['_dispatchEvent'](ScrollView.EventType.SCROLL_ENG_WITH_THRESHOLD);
  561. sv['_isScrollEndedWithThresholdEventFired'] = true;
  562. }
  563. if (sv['elastic']) {
  564. const brakeOffsetPosition = clonedAutoScrollStartPosition.clone();
  565. brakeOffsetPosition.subtract(sv['_autoScrollBrakingStartPosition']);
  566. if (isAutoScrollBrake) {
  567. brakeOffsetPosition.multiplyScalar(brakingFactor);
  568. }
  569. clonedAutoScrollStartPosition.set(sv['_autoScrollBrakingStartPosition']);
  570. clonedAutoScrollStartPosition.add(brakeOffsetPosition);
  571. } else {
  572. const moveDelta = clonedAutoScrollStartPosition.clone();
  573. moveDelta.subtract(sv['_getContentPosition']());
  574. const outOfBoundary = sv['_getHowMuchOutOfBoundary'](moveDelta);
  575. if (!outOfBoundary.equals(ZERO, EPSILON)) {
  576. clonedAutoScrollStartPosition.add(outOfBoundary);
  577. reachedEnd = true;
  578. }
  579. }
  580. if (reachedEnd) {
  581. sv['_autoScrolling'] = false;
  582. }
  583. const deltaMove = new Vec3(clonedAutoScrollStartPosition);
  584. deltaMove.subtract(sv['_getContentPosition']());
  585. sv['_clampDelta'](deltaMove);
  586. sv['_moveContent'](deltaMove, reachedEnd);
  587. sv['_dispatchEvent'](ScrollView.EventType.SCROLLING);
  588. if (!sv['_autoScrolling']) {
  589. sv['_isBouncing'] = false;
  590. sv['_scrolling'] = false;
  591. sv['_dispatchEvent'](ScrollView.EventType.SCROLL_ENDED);
  592. }
  593. }
  594. //设置模板Item
  595. setTemplateItem(item: any) {
  596. if (!item)
  597. return;
  598. let t: any = this;
  599. t._itemTmp = item;
  600. t._itemTmpUt = item.getComponent(UITransform);
  601. t._oScale = item.scale;
  602. if (t._resizeMode == Layout.ResizeMode.CHILDREN)
  603. t._itemSize = t._layout.cellSize;
  604. else {
  605. let itemUt: UITransform = item.getComponent(UITransform);
  606. t._itemSize = new Size(itemUt.width, itemUt.height);
  607. }
  608. //获取ListItem,如果没有就取消选择模式
  609. let com: any = item.getComponent(ListItem);
  610. let remove = false;
  611. if (!com)
  612. remove = true;
  613. // if (com) {
  614. // if (!com._btnCom && !item.getComponent(cc.Button)) {
  615. // remove = true;
  616. // }
  617. // }
  618. if (remove) {
  619. t.selectedMode = SelectedType.NONE;
  620. }
  621. com = item.getComponent(Widget);
  622. if (com && com.enabled) {
  623. t._needUpdateWidget = true;
  624. }
  625. if (t.selectedMode == SelectedType.MULT)
  626. t.multSelected = [];
  627. switch (t._align) {
  628. case Layout.Type.HORIZONTAL:
  629. t._colLineNum = 1;
  630. t._sizeType = false;
  631. break;
  632. case Layout.Type.VERTICAL:
  633. t._colLineNum = 1;
  634. t._sizeType = true;
  635. break;
  636. case Layout.Type.GRID:
  637. switch (t._startAxis) {
  638. case Layout.AxisDirection.HORIZONTAL:
  639. //计算列数
  640. let trimW: number = t._contentUt.width - t._leftGap - t._rightGap;
  641. t._colLineNum = Math.floor((trimW + t._columnGap) / (t._itemSize.width + t._columnGap));
  642. t._sizeType = true;
  643. break;
  644. case Layout.AxisDirection.VERTICAL:
  645. //计算行数
  646. let trimH: number = t._contentUt.height - t._topGap - t._bottomGap;
  647. t._colLineNum = Math.floor((trimH + t._lineGap) / (t._itemSize.height + t._lineGap));
  648. t._sizeType = false;
  649. break;
  650. }
  651. break;
  652. }
  653. }
  654. /**
  655. * 检查是否初始化
  656. * @param {Boolean} printLog 是否打印错误信息
  657. * @returns
  658. */
  659. checkInited(printLog: boolean = true) {
  660. if (!this._inited) {
  661. if (printLog)
  662. console.error('List initialization not completed!');
  663. return false;
  664. }
  665. return true;
  666. }
  667. //禁用 Layout 组件,自行计算 Content Size
  668. _resizeContent() {
  669. let t: any = this;
  670. let result: number;
  671. switch (t._align) {
  672. case Layout.Type.HORIZONTAL: {
  673. if (t._customSize) {
  674. let fixed: any = t._getFixedSize(null);
  675. result = t._leftGap + fixed.val + (t._itemSize.width * (t._numItems - fixed.count)) + (t._columnGap * (t._numItems - 1)) + t._rightGap;
  676. } else {
  677. result = t._leftGap + (t._itemSize.width * t._numItems) + (t._columnGap * (t._numItems - 1)) + t._rightGap;
  678. }
  679. break;
  680. }
  681. case Layout.Type.VERTICAL: {
  682. if (t._customSize) {
  683. let fixed: any = t._getFixedSize(null);
  684. result = t._topGap + fixed.val + (t._itemSize.height * (t._numItems - fixed.count)) + (t._lineGap * (t._numItems - 1)) + t._bottomGap;
  685. } else {
  686. result = t._topGap + (t._itemSize.height * t._numItems) + (t._lineGap * (t._numItems - 1)) + t._bottomGap;
  687. }
  688. break;
  689. }
  690. case Layout.Type.GRID: {
  691. //网格模式不支持居中
  692. if (t.lackCenter)
  693. t.lackCenter = false;
  694. switch (t._startAxis) {
  695. case Layout.AxisDirection.HORIZONTAL:
  696. let lineNum: number = Math.ceil(t._numItems / t._colLineNum);
  697. result = t._topGap + (t._itemSize.height * lineNum) + (t._lineGap * (lineNum - 1)) + t._bottomGap;
  698. break;
  699. case Layout.AxisDirection.VERTICAL:
  700. let colNum: number = Math.ceil(t._numItems / t._colLineNum);
  701. result = t._leftGap + (t._itemSize.width * colNum) + (t._columnGap * (colNum - 1)) + t._rightGap;
  702. break;
  703. }
  704. break;
  705. }
  706. }
  707. let layout: Layout = t.content.getComponent(Layout);
  708. if (layout)
  709. layout.enabled = false;
  710. t._allItemSize = result;
  711. t._allItemSizeNoEdge = t._allItemSize - (t._sizeType ? (t._topGap + t._bottomGap) : (t._leftGap + t._rightGap));
  712. if (t.cyclic) {
  713. let totalSize: number = (t._sizeType ? t._thisNodeUt.height : t._thisNodeUt.width);
  714. t._cyclicPos1 = 0;
  715. totalSize -= t._cyclicPos1;
  716. t._cyclicNum = Math.ceil(totalSize / t._allItemSizeNoEdge) + 1;
  717. let spacing: number = t._sizeType ? t._lineGap : t._columnGap;
  718. t._cyclicPos2 = t._cyclicPos1 + t._allItemSizeNoEdge + spacing;
  719. t._cyclicAllItemSize = t._allItemSize + (t._allItemSizeNoEdge * (t._cyclicNum - 1)) + (spacing * (t._cyclicNum - 1));
  720. t._cycilcAllItemSizeNoEdge = t._allItemSizeNoEdge * t._cyclicNum;
  721. t._cycilcAllItemSizeNoEdge += spacing * (t._cyclicNum - 1);
  722. // cc.log('_cyclicNum ->', t._cyclicNum, t._allItemSizeNoEdge, t._allItemSize, t._cyclicPos1, t._cyclicPos2);
  723. }
  724. t._lack = !t.cyclic && t._allItemSize < (t._sizeType ? t._thisNodeUt.height : t._thisNodeUt.width);
  725. let slideOffset: number = ((!t._lack || !t.lackCenter) && t.lackSlide) ? 0 : .1;
  726. let targetWH: number = t._lack ? ((t._sizeType ? t._thisNodeUt.height : t._thisNodeUt.width) - slideOffset) : (t.cyclic ? t._cyclicAllItemSize : t._allItemSize);
  727. if (targetWH < 0)
  728. targetWH = 0;
  729. if (t._sizeType) {
  730. t._contentUt.height = targetWH;
  731. } else {
  732. t._contentUt.width = targetWH;
  733. }
  734. // cc.log('_resizeContent() numItems =', t._numItems, ',content =', t.content);
  735. }
  736. /**
  737. * 自定义大小
  738. * @returns
  739. */
  740. _getChildSize(): Size{
  741. let pTra: UITransform = this.content.getComponent(UITransform);
  742. let spacingX: number = this._layout.spacingX;
  743. let paddingLeft: number = this._layout.paddingLeft;
  744. let w: number = (pTra.contentSize.width - (this._numItems - 1) * spacingX - paddingLeft * 2) / this._numItems;
  745. return new Size(w,w);
  746. }
  747. //滚动进行时...
  748. _onScrolling(ev: Event = null) {
  749. if (this.frameCount == null)
  750. this.frameCount = this._updateRate;
  751. if (!this._forceUpdate && (ev && ev.type != 'scroll-ended') && this.frameCount > 0) {
  752. this.frameCount--;
  753. return;
  754. } else
  755. this.frameCount = this._updateRate;
  756. if (this._aniDelRuning)
  757. return;
  758. //循环列表处理
  759. if (this.cyclic) {
  760. let scrollPos: any = this.content.getPosition();
  761. scrollPos = this._sizeType ? scrollPos.y : scrollPos.x;
  762. let addVal = this._allItemSizeNoEdge + (this._sizeType ? this._lineGap : this._columnGap);
  763. let add: any = this._sizeType ? new Vec3(0, addVal, 0) : new Vec3(addVal, 0, 0);
  764. let contentPos = this.content.getPosition();
  765. switch (this._alignCalcType) {
  766. case 1://单行HORIZONTAL(LEFT_TO_RIGHT)、网格VERTICAL(LEFT_TO_RIGHT)
  767. if (scrollPos > -this._cyclicPos1) {
  768. contentPos.set(-this._cyclicPos2, contentPos.y, contentPos.z);
  769. this.content.setPosition(contentPos);
  770. if (this._scrollView.isAutoScrolling()) {
  771. this._scrollView['_autoScrollStartPosition'] = this._scrollView['_autoScrollStartPosition'].subtract(add);
  772. }
  773. // if (this._beganPos) {
  774. // this._beganPos += add;
  775. // }
  776. } else if (scrollPos < -this._cyclicPos2) {
  777. contentPos.set(-this._cyclicPos1, contentPos.y, contentPos.z);
  778. this.content.setPosition(contentPos);
  779. if (this._scrollView.isAutoScrolling()) {
  780. this._scrollView['_autoScrollStartPosition'] = this._scrollView['_autoScrollStartPosition'].add(add);
  781. }
  782. // if (this._beganPos) {
  783. // this._beganPos -= add;
  784. // }
  785. }
  786. break;
  787. case 2://单行HORIZONTAL(RIGHT_TO_LEFT)、网格VERTICAL(RIGHT_TO_LEFT)
  788. if (scrollPos < this._cyclicPos1) {
  789. contentPos.set(this._cyclicPos2, contentPos.y, contentPos.z);
  790. this.content.setPosition(contentPos);
  791. if (this._scrollView.isAutoScrolling()) {
  792. this._scrollView['_autoScrollStartPosition'] = this._scrollView['_autoScrollStartPosition'].add(add);
  793. }
  794. } else if (scrollPos > this._cyclicPos2) {
  795. contentPos.set(this._cyclicPos1, contentPos.y, contentPos.z);
  796. this.content.setPosition(contentPos);
  797. if (this._scrollView.isAutoScrolling()) {
  798. this._scrollView['_autoScrollStartPosition'] = this._scrollView['_autoScrollStartPosition'].subtract(add);
  799. }
  800. }
  801. break;
  802. case 3://单列VERTICAL(TOP_TO_BOTTOM)、网格HORIZONTAL(TOP_TO_BOTTOM)
  803. if (scrollPos < this._cyclicPos1) {
  804. contentPos.set(contentPos.x, this._cyclicPos2, contentPos.z);
  805. this.content.setPosition(contentPos);
  806. if (this._scrollView.isAutoScrolling()) {
  807. this._scrollView['_autoScrollStartPosition'] = this._scrollView['_autoScrollStartPosition'].add(add);
  808. }
  809. } else if (scrollPos > this._cyclicPos2) {
  810. contentPos.set(contentPos.x, this._cyclicPos1, contentPos.z);
  811. this.content.setPosition(contentPos);
  812. if (this._scrollView.isAutoScrolling()) {
  813. this._scrollView['_autoScrollStartPosition'] = this._scrollView['_autoScrollStartPosition'].subtract(add);
  814. }
  815. }
  816. break;
  817. case 4://单列VERTICAL(BOTTOM_TO_TOP)、网格HORIZONTAL(BOTTOM_TO_TOP)
  818. if (scrollPos > -this._cyclicPos1) {
  819. contentPos.set(contentPos.x, -this._cyclicPos2, contentPos.z);
  820. this.content.setPosition(contentPos);
  821. if (this._scrollView.isAutoScrolling()) {
  822. this._scrollView['_autoScrollStartPosition'] = this._scrollView['_autoScrollStartPosition'].subtract(add);
  823. }
  824. } else if (scrollPos < -this._cyclicPos2) {
  825. contentPos.set(contentPos.x, -this._cyclicPos1, contentPos.z);
  826. this.content.setPosition(contentPos);
  827. if (this._scrollView.isAutoScrolling()) {
  828. this._scrollView['_autoScrollStartPosition'] = this._scrollView['_autoScrollStartPosition'].add(add);
  829. }
  830. }
  831. break;
  832. }
  833. }
  834. this._calcViewPos();
  835. let vTop: number, vRight: number, vBottom: number, vLeft: number;
  836. if (this._sizeType) {
  837. vTop = this.viewTop;
  838. vBottom = this.viewBottom;
  839. } else {
  840. vRight = this.viewRight;
  841. vLeft = this.viewLeft;
  842. }
  843. if (this._virtual) {
  844. this.displayData = [];
  845. let itemPos: any;
  846. let curId: number = 0;
  847. let endId: number = this._numItems - 1;
  848. if (this._customSize) {
  849. let breakFor: boolean = false;
  850. //如果该item的位置在可视区域内,就推入displayData
  851. for (; curId <= endId && !breakFor; curId++) {
  852. itemPos = this._calcItemPos(curId);
  853. switch (this._align) {
  854. case Layout.Type.HORIZONTAL:
  855. if (itemPos.right >= vLeft && itemPos.left <= vRight) {
  856. this.displayData.push(itemPos);
  857. } else if (curId != 0 && this.displayData.length > 0) {
  858. breakFor = true;
  859. }
  860. break;
  861. case Layout.Type.VERTICAL:
  862. if (itemPos.bottom <= vTop && itemPos.top >= vBottom) {
  863. this.displayData.push(itemPos);
  864. } else if (curId != 0 && this.displayData.length > 0) {
  865. breakFor = true;
  866. }
  867. break;
  868. case Layout.Type.GRID:
  869. switch (this._startAxis) {
  870. case Layout.AxisDirection.HORIZONTAL:
  871. if (itemPos.bottom <= vTop && itemPos.top >= vBottom) {
  872. this.displayData.push(itemPos);
  873. } else if (curId != 0 && this.displayData.length > 0) {
  874. breakFor = true;
  875. }
  876. break;
  877. case Layout.AxisDirection.VERTICAL:
  878. if (itemPos.right >= vLeft && itemPos.left <= vRight) {
  879. this.displayData.push(itemPos);
  880. } else if (curId != 0 && this.displayData.length > 0) {
  881. breakFor = true;
  882. }
  883. break;
  884. }
  885. break;
  886. }
  887. }
  888. } else {
  889. let ww: number = this._itemSize.width + this._columnGap;
  890. let hh: number = this._itemSize.height + this._lineGap;
  891. switch (this._alignCalcType) {
  892. case 1://单行HORIZONTAL(LEFT_TO_RIGHT)、网格VERTICAL(LEFT_TO_RIGHT)
  893. curId = (vLeft - this._leftGap) / ww;
  894. endId = (vRight - this._leftGap) / ww;
  895. break;
  896. case 2://单行HORIZONTAL(RIGHT_TO_LEFT)、网格VERTICAL(RIGHT_TO_LEFT)
  897. curId = (-vRight - this._rightGap) / ww;
  898. endId = (-vLeft - this._rightGap) / ww;
  899. break;
  900. case 3://单列VERTICAL(TOP_TO_BOTTOM)、网格HORIZONTAL(TOP_TO_BOTTOM)
  901. curId = (-vTop - this._topGap) / hh;
  902. endId = (-vBottom - this._topGap) / hh;
  903. break;
  904. case 4://单列VERTICAL(BOTTOM_TO_TOP)、网格HORIZONTAL(BOTTOM_TO_TOP)
  905. curId = (vBottom - this._bottomGap) / hh;
  906. endId = (vTop - this._bottomGap) / hh;
  907. break;
  908. }
  909. curId = Math.floor(curId) * this._colLineNum;
  910. endId = Math.ceil(endId) * this._colLineNum;
  911. endId--;
  912. if (curId < 0)
  913. curId = 0;
  914. if (endId >= this._numItems)
  915. endId = this._numItems - 1;
  916. for (; curId <= endId; curId++) {
  917. this.displayData.push(this._calcItemPos(curId));
  918. }
  919. }
  920. this._delRedundantItem();
  921. if (this.displayData.length <= 0 || !this._numItems) { //if none, delete all.
  922. this._lastDisplayData = [];
  923. return;
  924. }
  925. this.firstListId = this.displayData[0].id;
  926. this.displayItemNum = this.displayData.length;
  927. let len: number = this._lastDisplayData.length;
  928. let haveDataChange: boolean = this.displayItemNum != len;
  929. if (haveDataChange) {
  930. // 如果是逐帧渲染,需要排序
  931. if (this.frameByFrameRenderNum > 0) {
  932. this._lastDisplayData.sort((a, b) => { return a - b });
  933. }
  934. // 因List的显示数据是有序的,所以只需要判断数组长度是否相等,以及头、尾两个元素是否相等即可。
  935. haveDataChange = this.firstListId != this._lastDisplayData[0] || this.displayData[this.displayItemNum - 1].id != this._lastDisplayData[len - 1];
  936. }
  937. if (this._forceUpdate || haveDataChange) { //如果是强制更新
  938. if (this.frameByFrameRenderNum > 0) {
  939. // if (this._updateDone) {
  940. // this._lastDisplayData = [];
  941. //逐帧渲染
  942. if (this._numItems > 0) {
  943. if (!this._updateDone) {
  944. this._doneAfterUpdate = true;
  945. } else {
  946. this._updateCounter = 0;
  947. }
  948. this._updateDone = false;
  949. } else {
  950. this._updateCounter = 0;
  951. this._updateDone = true;
  952. }
  953. // }
  954. } else {
  955. //直接渲染
  956. this._lastDisplayData = [];
  957. // cc.log('List Display Data II::', this.displayData);
  958. for (let c = 0; c < this.displayItemNum; c++) {
  959. this._createOrUpdateItem(this.displayData[c]);
  960. }
  961. this._forceUpdate = false;
  962. }
  963. }
  964. this._calcNearestItem();
  965. }
  966. }
  967. //计算可视范围
  968. _calcViewPos() {
  969. let scrollPos: any = this.content.getPosition();
  970. switch (this._alignCalcType) {
  971. case 1://单行HORIZONTAL(LEFT_TO_RIGHT)、网格VERTICAL(LEFT_TO_RIGHT)
  972. this.elasticLeft = scrollPos.x > 0 ? scrollPos.x : 0;
  973. this.viewLeft = (scrollPos.x < 0 ? -scrollPos.x : 0) - this.elasticLeft;
  974. this.viewRight = this.viewLeft + this._thisNodeUt.width;
  975. this.elasticRight = this.viewRight > this._contentUt.width ? Math.abs(this.viewRight - this._contentUt.width) : 0;
  976. this.viewRight += this.elasticRight;
  977. // cc.log(this.elasticLeft, this.elasticRight, this.viewLeft, this.viewRight);
  978. break;
  979. case 2://单行HORIZONTAL(RIGHT_TO_LEFT)、网格VERTICAL(RIGHT_TO_LEFT)
  980. this.elasticRight = scrollPos.x < 0 ? -scrollPos.x : 0;
  981. this.viewRight = (scrollPos.x > 0 ? -scrollPos.x : 0) + this.elasticRight;
  982. this.viewLeft = this.viewRight - this._thisNodeUt.width;
  983. this.elasticLeft = this.viewLeft < -this._contentUt.width ? Math.abs(this.viewLeft + this._contentUt.width) : 0;
  984. this.viewLeft -= this.elasticLeft;
  985. // cc.log(this.elasticLeft, this.elasticRight, this.viewLeft, this.viewRight);
  986. break;
  987. case 3://单列VERTICAL(TOP_TO_BOTTOM)、网格HORIZONTAL(TOP_TO_BOTTOM)
  988. this.elasticTop = scrollPos.y < 0 ? Math.abs(scrollPos.y) : 0;
  989. this.viewTop = (scrollPos.y > 0 ? -scrollPos.y : 0) + this.elasticTop;
  990. this.viewBottom = this.viewTop - this._thisNodeUt.height;
  991. this.elasticBottom = this.viewBottom < -this._contentUt.height ? Math.abs(this.viewBottom + this._contentUt.height) : 0;
  992. this.viewBottom += this.elasticBottom;
  993. // cc.log(this.elasticTop, this.elasticBottom, this.viewTop, this.viewBottom);
  994. break;
  995. case 4://单列VERTICAL(BOTTOM_TO_TOP)、网格HORIZONTAL(BOTTOM_TO_TOP)
  996. this.elasticBottom = scrollPos.y > 0 ? Math.abs(scrollPos.y) : 0;
  997. this.viewBottom = (scrollPos.y < 0 ? -scrollPos.y : 0) - this.elasticBottom;
  998. this.viewTop = this.viewBottom + this._thisNodeUt.height;
  999. this.elasticTop = this.viewTop > this._contentUt.height ? Math.abs(this.viewTop - this._contentUt.height) : 0;
  1000. this.viewTop -= this.elasticTop;
  1001. // cc.log(this.elasticTop, this.elasticBottom, this.viewTop, this.viewBottom);
  1002. break;
  1003. }
  1004. }
  1005. //计算位置 根据id
  1006. _calcItemPos(id: number) {
  1007. let width: number, height: number, top: number, bottom: number, left: number, right: number, itemX: number, itemY: number;
  1008. switch (this._align) {
  1009. case Layout.Type.HORIZONTAL:
  1010. switch (this._horizontalDir) {
  1011. case Layout.HorizontalDirection.LEFT_TO_RIGHT: {
  1012. if (this._customSize) {
  1013. let fixed: any = this._getFixedSize(id);
  1014. left = this._leftGap + ((this._itemSize.width + this._columnGap) * (id - fixed.count)) + (fixed.val + (this._columnGap * fixed.count));
  1015. let cs: number = this._customSize[id];
  1016. width = (cs > 0 ? cs : this._itemSize.width);
  1017. } else {
  1018. left = this._leftGap + ((this._itemSize.width + this._columnGap) * id);
  1019. width = this._itemSize.width;
  1020. }
  1021. if (this.lackCenter) {
  1022. left -= this._leftGap;
  1023. let offset: number = (this._contentUt.width / 2) - (this._allItemSizeNoEdge / 2);
  1024. left += offset;
  1025. }
  1026. right = left + width;
  1027. return {
  1028. id: id,
  1029. left: left,
  1030. right: right,
  1031. x: left + (this._itemTmpUt.anchorX * width),
  1032. y: this._itemTmp.y,
  1033. };
  1034. }
  1035. case Layout.HorizontalDirection.RIGHT_TO_LEFT: {
  1036. if (this._customSize) {
  1037. let fixed: any = this._getFixedSize(id);
  1038. right = -this._rightGap - ((this._itemSize.width + this._columnGap) * (id - fixed.count)) - (fixed.val + (this._columnGap * fixed.count));
  1039. let cs: number = this._customSize[id];
  1040. width = (cs > 0 ? cs : this._itemSize.width);
  1041. } else {
  1042. right = -this._rightGap - ((this._itemSize.width + this._columnGap) * id);
  1043. width = this._itemSize.width;
  1044. }
  1045. if (this.lackCenter) {
  1046. right += this._rightGap;
  1047. let offset: number = (this._contentUt.width / 2) - (this._allItemSizeNoEdge / 2);
  1048. right -= offset;
  1049. }
  1050. left = right - width;
  1051. return {
  1052. id: id,
  1053. right: right,
  1054. left: left,
  1055. x: left + (this._itemTmpUt.anchorX * width),
  1056. y: this._itemTmp.y,
  1057. };
  1058. }
  1059. }
  1060. break;
  1061. case Layout.Type.VERTICAL: {
  1062. switch (this._verticalDir) {
  1063. case Layout.VerticalDirection.TOP_TO_BOTTOM: {
  1064. if (this._customSize) {
  1065. let fixed: any = this._getFixedSize(id);
  1066. top = -this._topGap - ((this._itemSize.height + this._lineGap) * (id - fixed.count)) - (fixed.val + (this._lineGap * fixed.count));
  1067. let cs: number = this._customSize[id];
  1068. height = (cs > 0 ? cs : this._itemSize.height);
  1069. } else {
  1070. top = -this._topGap - ((this._itemSize.height + this._lineGap) * id);
  1071. height = this._itemSize.height;
  1072. }
  1073. if (this.lackCenter) {
  1074. top += this._topGap;
  1075. let offset: number = (this._contentUt.height / 2) - (this._allItemSizeNoEdge / 2);
  1076. top -= offset;
  1077. }
  1078. bottom = top - height;
  1079. return {
  1080. id: id,
  1081. top: top,
  1082. bottom: bottom,
  1083. x: this._itemTmp.x,
  1084. y: bottom + (this._itemTmpUt.anchorY * height),
  1085. };
  1086. }
  1087. case Layout.VerticalDirection.BOTTOM_TO_TOP: {
  1088. if (this._customSize) {
  1089. let fixed: any = this._getFixedSize(id);
  1090. bottom = this._bottomGap + ((this._itemSize.height + this._lineGap) * (id - fixed.count)) + (fixed.val + (this._lineGap * fixed.count));
  1091. let cs: number = this._customSize[id];
  1092. height = (cs > 0 ? cs : this._itemSize.height);
  1093. } else {
  1094. bottom = this._bottomGap + ((this._itemSize.height + this._lineGap) * id);
  1095. height = this._itemSize.height;
  1096. }
  1097. if (this.lackCenter) {
  1098. bottom -= this._bottomGap;
  1099. let offset: number = (this._contentUt.height / 2) - (this._allItemSizeNoEdge / 2);
  1100. bottom += offset;
  1101. }
  1102. top = bottom + height;
  1103. return {
  1104. id: id,
  1105. top: top,
  1106. bottom: bottom,
  1107. x: this._itemTmp.x,
  1108. y: bottom - (this._itemTmpUt.anchorY * height),
  1109. };
  1110. break;
  1111. }
  1112. }
  1113. }
  1114. case Layout.Type.GRID: {
  1115. let colLine: number = Math.floor(id / this._colLineNum);
  1116. switch (this._startAxis) {
  1117. case Layout.AxisDirection.HORIZONTAL: {
  1118. switch (this._verticalDir) {
  1119. case Layout.VerticalDirection.TOP_TO_BOTTOM: {
  1120. top = -this._topGap - ((this._itemSize.height + this._lineGap) * colLine);
  1121. bottom = top - this._itemSize.height;
  1122. itemY = bottom + (this._itemTmpUt.anchorY * this._itemSize.height);
  1123. break;
  1124. }
  1125. case Layout.VerticalDirection.BOTTOM_TO_TOP: {
  1126. bottom = this._bottomGap + ((this._itemSize.height + this._lineGap) * colLine);
  1127. top = bottom + this._itemSize.height;
  1128. itemY = bottom + (this._itemTmpUt.anchorY * this._itemSize.height);
  1129. break;
  1130. }
  1131. }
  1132. itemX = this._leftGap + ((id % this._colLineNum) * (this._itemSize.width + this._columnGap));
  1133. switch (this._horizontalDir) {
  1134. case Layout.HorizontalDirection.LEFT_TO_RIGHT: {
  1135. itemX += (this._itemTmpUt.anchorX * this._itemSize.width);
  1136. itemX -= (this._contentUt.anchorX * this._contentUt.width);
  1137. break;
  1138. }
  1139. case Layout.HorizontalDirection.RIGHT_TO_LEFT: {
  1140. itemX += ((1 - this._itemTmpUt.anchorX) * this._itemSize.width);
  1141. itemX -= ((1 - this._contentUt.anchorX) * this._contentUt.width);
  1142. itemX *= -1;
  1143. break;
  1144. }
  1145. }
  1146. return {
  1147. id: id,
  1148. top: top,
  1149. bottom: bottom,
  1150. x: itemX,
  1151. y: itemY,
  1152. };
  1153. }
  1154. case Layout.AxisDirection.VERTICAL: {
  1155. switch (this._horizontalDir) {
  1156. case Layout.HorizontalDirection.LEFT_TO_RIGHT: {
  1157. left = this._leftGap + ((this._itemSize.width + this._columnGap) * colLine);
  1158. right = left + this._itemSize.width;
  1159. itemX = left + (this._itemTmpUt.anchorX * this._itemSize.width);
  1160. itemX -= (this._contentUt.anchorX * this._contentUt.width);
  1161. break;
  1162. }
  1163. case Layout.HorizontalDirection.RIGHT_TO_LEFT: {
  1164. right = -this._rightGap - ((this._itemSize.width + this._columnGap) * colLine);
  1165. left = right - this._itemSize.width;
  1166. itemX = left + (this._itemTmpUt.anchorX * this._itemSize.width);
  1167. itemX += ((1 - this._contentUt.anchorX) * this._contentUt.width);
  1168. break;
  1169. }
  1170. }
  1171. itemY = -this._topGap - ((id % this._colLineNum) * (this._itemSize.height + this._lineGap));
  1172. switch (this._verticalDir) {
  1173. case Layout.VerticalDirection.TOP_TO_BOTTOM: {
  1174. itemY -= ((1 - this._itemTmpUt.anchorY) * this._itemSize.height);
  1175. itemY += ((1 - this._contentUt.anchorY) * this._contentUt.height);
  1176. break;
  1177. }
  1178. case Layout.VerticalDirection.BOTTOM_TO_TOP: {
  1179. itemY -= ((this._itemTmpUt.anchorY) * this._itemSize.height);
  1180. itemY += (this._contentUt.anchorY * this._contentUt.height);
  1181. itemY *= -1;
  1182. break;
  1183. }
  1184. }
  1185. return {
  1186. id: id,
  1187. left: left,
  1188. right: right,
  1189. x: itemX,
  1190. y: itemY,
  1191. };
  1192. }
  1193. }
  1194. break;
  1195. }
  1196. }
  1197. }
  1198. //计算已存在的Item的位置
  1199. _calcExistItemPos(id: number) {
  1200. let item: any = this.getItemByListId(id);
  1201. if (!item)
  1202. return null;
  1203. let ut: UITransform = item.getComponent(UITransform);
  1204. let pos: Vec3 = item.getPosition();
  1205. let data: any = {
  1206. id: id,
  1207. x: pos.x,
  1208. y: pos.y,
  1209. }
  1210. if (this._sizeType) {
  1211. data.top = pos.y + (ut.height * (1 - ut.anchorY));
  1212. data.bottom = pos.y - (ut.height * ut.anchorY);
  1213. } else {
  1214. data.left = pos.x - (ut.width * ut.anchorX);
  1215. data.right = pos.x + (ut.width * (1 - ut.anchorX));
  1216. }
  1217. return data;
  1218. }
  1219. //获取Item位置
  1220. getItemPos(id: number) {
  1221. if (this._virtual)
  1222. return this._calcItemPos(id);
  1223. else {
  1224. if (this.frameByFrameRenderNum)
  1225. return this._calcItemPos(id);
  1226. else
  1227. return this._calcExistItemPos(id);
  1228. }
  1229. }
  1230. //获取固定尺寸
  1231. _getFixedSize(listId: number) {
  1232. if (!this._customSize)
  1233. return null;
  1234. if (listId == null)
  1235. listId = this._numItems;
  1236. let fixed: number = 0;
  1237. let count: number = 0;
  1238. for (let id in this._customSize) {
  1239. if (parseInt(id) < listId) {
  1240. fixed += this._customSize[id];
  1241. count++;
  1242. }
  1243. }
  1244. return {
  1245. val: fixed,
  1246. count: count,
  1247. }
  1248. }
  1249. //滚动结束时..
  1250. _onScrollBegan() {
  1251. this._beganPos = this._sizeType ? this.viewTop : this.viewLeft;
  1252. }
  1253. //滚动结束时..
  1254. _onScrollEnded() {
  1255. let t: any = this;
  1256. t._curScrollIsTouch = false;
  1257. if (t.scrollToListId != null) {
  1258. let item: any = t.getItemByListId(t.scrollToListId);
  1259. t.scrollToListId = null;
  1260. if (item) {
  1261. tween(item)
  1262. .to(.1, { scale: 1.06 })
  1263. .to(.1, { scale: 1 })
  1264. .start();
  1265. }
  1266. }
  1267. t._onScrolling();
  1268. if (t._slideMode == SlideType.ADHERING &&
  1269. !t.adhering
  1270. ) {
  1271. //cc.log(t.adhering, t._scrollView.isAutoScrolling(), t._scrollView.isScrolling());
  1272. t.adhere();
  1273. } else if (t._slideMode == SlideType.PAGE) {
  1274. if (t._beganPos != null && t._curScrollIsTouch) {
  1275. this._pageAdhere();
  1276. } else {
  1277. t.adhere();
  1278. }
  1279. }
  1280. }
  1281. // 触摸时
  1282. _onTouchStart(ev, captureListeners) {
  1283. if (this._scrollView['_hasNestedViewGroup'](ev, captureListeners))
  1284. return;
  1285. this._curScrollIsTouch = true;
  1286. let isMe = ev.eventPhase === Event.AT_TARGET && ev.target === this.node;
  1287. if (!isMe) {
  1288. let itemNode: any = ev.target;
  1289. while (itemNode._listId == null && itemNode.parent)
  1290. itemNode = itemNode.parent;
  1291. this._scrollItem = itemNode._listId != null ? itemNode : ev.target;
  1292. }
  1293. }
  1294. //触摸抬起时..
  1295. _onTouchUp() {
  1296. let t: any = this;
  1297. t._scrollPos = null;
  1298. if (t._slideMode == SlideType.ADHERING) {
  1299. if (this.adhering)
  1300. this._adheringBarrier = true;
  1301. t.adhere();
  1302. } else if (t._slideMode == SlideType.PAGE) {
  1303. if (t._beganPos != null) {
  1304. this._pageAdhere();
  1305. } else {
  1306. t.adhere();
  1307. }
  1308. }
  1309. this._scrollItem = null;
  1310. }
  1311. _onTouchCancelled(ev, captureListeners) {
  1312. let t = this;
  1313. if (t._scrollView['_hasNestedViewGroup'](ev, captureListeners) || ev.simulate)
  1314. return;
  1315. t._scrollPos = null;
  1316. if (t._slideMode == SlideType.ADHERING) {
  1317. if (t.adhering)
  1318. t._adheringBarrier = true;
  1319. t.adhere();
  1320. } else if (t._slideMode == SlideType.PAGE) {
  1321. if (t._beganPos != null) {
  1322. t._pageAdhere();
  1323. } else {
  1324. t.adhere();
  1325. }
  1326. }
  1327. this._scrollItem = null;
  1328. }
  1329. //当尺寸改变
  1330. _onSizeChanged() {
  1331. if (this.checkInited(false))
  1332. this._onScrolling();
  1333. }
  1334. //当Item自适应
  1335. _onItemAdaptive(item: any) {
  1336. let ut: UITransform = item.getComponent(UITransform);
  1337. // if (this.checkInited(false)) {
  1338. if (
  1339. (!this._sizeType && ut.width != this._itemSize.width)
  1340. || (this._sizeType && ut.height != this._itemSize.height)
  1341. ) {
  1342. if (!this._customSize)
  1343. this._customSize = {};
  1344. let val = this._sizeType ? ut.height : ut.width;
  1345. if (this._customSize[item._listId] != val) {
  1346. this._customSize[item._listId] = val;
  1347. this._resizeContent();
  1348. // this.content.children.forEach((child: Node) => {
  1349. // this._updateItemPos(child);
  1350. // });
  1351. this.updateAll();
  1352. // 如果当前正在运行 scrollTo,肯定会不准确,在这里做修正
  1353. if (this._scrollToListId != null) {
  1354. this._scrollPos = null;
  1355. this.unschedule(this._scrollToSo);
  1356. this.scrollTo(this._scrollToListId, Math.max(0, this._scrollToEndTime - ((new Date()).getTime() / 1000)));
  1357. }
  1358. }
  1359. }
  1360. // }
  1361. }
  1362. //PAGE粘附
  1363. _pageAdhere() {
  1364. let t = this;
  1365. if (!t.cyclic && (t.elasticTop > 0 || t.elasticRight > 0 || t.elasticBottom > 0 || t.elasticLeft > 0))
  1366. return;
  1367. let curPos = t._sizeType ? t.viewTop : t.viewLeft;
  1368. let dis = (t._sizeType ? t._thisNodeUt.height : t._thisNodeUt.width) * t.pageDistance;
  1369. let canSkip = Math.abs(t._beganPos - curPos) > dis;
  1370. if (canSkip) {
  1371. let timeInSecond = .5;
  1372. switch (t._alignCalcType) {
  1373. case 1://单行HORIZONTAL(LEFT_TO_RIGHT)、网格VERTICAL(LEFT_TO_RIGHT)
  1374. case 4://单列VERTICAL(BOTTOM_TO_TOP)、网格HORIZONTAL(BOTTOM_TO_TOP)
  1375. if (t._beganPos > curPos) {
  1376. t.prePage(timeInSecond);
  1377. // cc.log('_pageAdhere PPPPPPPPPPPPPPP');
  1378. } else {
  1379. t.nextPage(timeInSecond);
  1380. // cc.log('_pageAdhere NNNNNNNNNNNNNNN');
  1381. }
  1382. break;
  1383. case 2://单行HORIZONTAL(RIGHT_TO_LEFT)、网格VERTICAL(RIGHT_TO_LEFT)
  1384. case 3://单列VERTICAL(TOP_TO_BOTTOM)、网格HORIZONTAL(TOP_TO_BOTTOM)
  1385. if (t._beganPos < curPos) {
  1386. t.prePage(timeInSecond);
  1387. } else {
  1388. t.nextPage(timeInSecond);
  1389. }
  1390. break;
  1391. }
  1392. } else if (t.elasticTop <= 0 && t.elasticRight <= 0 && t.elasticBottom <= 0 && t.elasticLeft <= 0) {
  1393. t.adhere();
  1394. }
  1395. t._beganPos = null;
  1396. }
  1397. //粘附
  1398. adhere() {
  1399. let t: any = this;
  1400. if (!t.checkInited())
  1401. return;
  1402. if (t.elasticTop > 0 || t.elasticRight > 0 || t.elasticBottom > 0 || t.elasticLeft > 0)
  1403. return;
  1404. t.adhering = true;
  1405. t._calcNearestItem();
  1406. let offset: number = (t._sizeType ? t._topGap : t._leftGap) / (t._sizeType ? t._thisNodeUt.height : t._thisNodeUt.width);
  1407. let timeInSecond: number = .7;
  1408. t.scrollTo(t.nearestListId, timeInSecond, offset);
  1409. }
  1410. //Update..
  1411. update() {
  1412. if (this.frameByFrameRenderNum <= 0 || this._updateDone)
  1413. return;
  1414. // cc.log(this.displayData.length, this._updateCounter, this.displayData[this._updateCounter]);
  1415. if (this._virtual) {
  1416. let len: number = (this._updateCounter + this.frameByFrameRenderNum) > this.displayItemNum ? this.displayItemNum : (this._updateCounter + this.frameByFrameRenderNum);
  1417. for (let n: number = this._updateCounter; n < len; n++) {
  1418. let data: any = this.displayData[n];
  1419. if (data) {
  1420. this._createOrUpdateItem(data);
  1421. }
  1422. }
  1423. if (this._updateCounter >= this.displayItemNum - 1) { //最后一个
  1424. if (this._doneAfterUpdate) {
  1425. this._updateCounter = 0;
  1426. this._updateDone = false;
  1427. // if (!this._scrollView.isScrolling())
  1428. this._doneAfterUpdate = false;
  1429. } else {
  1430. this._updateDone = true;
  1431. this._delRedundantItem();
  1432. this._forceUpdate = false;
  1433. this._calcNearestItem();
  1434. if (this.slideMode == SlideType.PAGE)
  1435. this.curPageNum = this.nearestListId;
  1436. }
  1437. } else {
  1438. this._updateCounter += this.frameByFrameRenderNum;
  1439. }
  1440. } else {
  1441. if (this._updateCounter < this._numItems) {
  1442. let len: number = (this._updateCounter + this.frameByFrameRenderNum) > this._numItems ? this._numItems : (this._updateCounter + this.frameByFrameRenderNum);
  1443. for (let n: number = this._updateCounter; n < len; n++) {
  1444. this._createOrUpdateItem2(n);
  1445. }
  1446. this._updateCounter += this.frameByFrameRenderNum;
  1447. } else {
  1448. this._updateDone = true;
  1449. this._calcNearestItem();
  1450. if (this.slideMode == SlideType.PAGE)
  1451. this.curPageNum = this.nearestListId;
  1452. }
  1453. }
  1454. }
  1455. /**
  1456. * 创建或更新Item(虚拟列表用)
  1457. * @param {Object} data 数据
  1458. */
  1459. _createOrUpdateItem(data: any) {
  1460. let item: any = this.getItemByListId(data.id);
  1461. if (!item) { //如果不存在
  1462. item = PoolManager.getNode(this._itemTmp,this.content);
  1463. if (item._listId != data.id) {
  1464. item._listId = data.id;
  1465. let ut: UITransform = item.getComponent(UITransform);
  1466. ut.setContentSize(this._itemSize);
  1467. }
  1468. item.setPosition(new Vec3(data.x, data.y, 0));
  1469. this._resetItemSize(item);
  1470. if (this._needUpdateWidget) {
  1471. let widget: Widget = item.getComponent(Widget);
  1472. if (widget)
  1473. widget.updateAlignment();
  1474. }
  1475. item.setSiblingIndex(this.content.children.length - 1);
  1476. let listItem: ListItem = item.getComponent(ListItem);
  1477. item['listItem'] = listItem;
  1478. if (listItem) {
  1479. listItem.listId = data.id;
  1480. listItem.list = this;
  1481. listItem._registerEvent();
  1482. }
  1483. if (this.renderEvent) {
  1484. EventHandler.emitEvents([this.renderEvent], item, data.id % this._actualNumItems);
  1485. }
  1486. } else if (this._forceUpdate && this.renderEvent) { //强制更新
  1487. item.setPosition(new Vec3(data.x, data.y, 0));
  1488. this._resetItemSize(item);
  1489. // cc.log('ADD::', data.id, item);
  1490. if (this.renderEvent) {
  1491. EventHandler.emitEvents([this.renderEvent], item, data.id % this._actualNumItems);
  1492. }
  1493. }
  1494. this._resetItemSize(item);
  1495. this._updateListItem(item['listItem']);
  1496. if (this._lastDisplayData.indexOf(data.id) < 0) {
  1497. this._lastDisplayData.push(data.id);
  1498. }
  1499. }
  1500. //创建或更新Item(非虚拟列表用)
  1501. _createOrUpdateItem2(listId: number) {
  1502. let item: any = this.content.children[listId];
  1503. let listItem: ListItem;
  1504. if (!item) { //如果不存在
  1505. item = PoolManager.getNode(this._itemTmp,this.content);
  1506. item._listId = listId;
  1507. listItem = item.getComponent(ListItem);
  1508. item['listItem'] = listItem;
  1509. if (listItem) {
  1510. listItem.listId = listId;
  1511. listItem.list = this;
  1512. listItem._registerEvent();
  1513. }
  1514. if (this.renderEvent) {
  1515. EventHandler.emitEvents([this.renderEvent], item, listId % this._actualNumItems);
  1516. }
  1517. } else if (this._forceUpdate && this.renderEvent) { //强制更新
  1518. item._listId = listId;
  1519. if (listItem)
  1520. listItem.listId = listId;
  1521. if (this.renderEvent) {
  1522. EventHandler.emitEvents([this.renderEvent], item, listId % this._actualNumItems);
  1523. }
  1524. }
  1525. this._updateListItem(listItem);
  1526. if (this._lastDisplayData.indexOf(listId) < 0) {
  1527. this._lastDisplayData.push(listId);
  1528. }
  1529. }
  1530. _updateListItem(listItem: ListItem) {
  1531. if (!listItem)
  1532. return;
  1533. if (this.selectedMode > SelectedType.NONE) {
  1534. let item: any = listItem.node;
  1535. switch (this.selectedMode) {
  1536. case SelectedType.SINGLE:
  1537. listItem.selected = this.selectedId == item._listId;
  1538. break;
  1539. case SelectedType.MULT:
  1540. listItem.selected = this.multSelected.indexOf(item._listId) >= 0;
  1541. break;
  1542. }
  1543. }
  1544. }
  1545. //仅虚拟列表用
  1546. _resetItemSize(item: any) {
  1547. return;
  1548. let size: number;
  1549. let ut: UITransform = item.getComponent(UITransform);
  1550. if (this._customSize && this._customSize[item._listId]) {
  1551. size = this._customSize[item._listId];
  1552. } else {
  1553. if (this._colLineNum > 1)
  1554. ut.setContentSize(this._itemSize);
  1555. else
  1556. size = this._sizeType ? this._itemSize.height : this._itemSize.width;
  1557. }
  1558. if (size) {
  1559. if (this._sizeType)
  1560. ut.height = size;
  1561. else
  1562. ut.width = size;
  1563. }
  1564. }
  1565. /**
  1566. * 更新Item位置
  1567. * @param {Number||Node} listIdOrItem
  1568. */
  1569. _updateItemPos(listIdOrItem: any) {
  1570. let item: any = isNaN(listIdOrItem) ? listIdOrItem : this.getItemByListId(listIdOrItem);
  1571. let pos: any = this.getItemPos(item._listId);
  1572. item.setPosition(pos.x, pos.y);
  1573. }
  1574. /**
  1575. * 设置多选
  1576. * @param {Array} args 可以是单个listId,也可是个listId数组
  1577. * @param {Boolean} bool 值,如果为null的话,则直接用args覆盖
  1578. */
  1579. setMultSelected(args: any, bool: boolean) {
  1580. let t: any = this;
  1581. if (!t.checkInited())
  1582. return;
  1583. if (!Array.isArray(args)) {
  1584. args = [args];
  1585. }
  1586. if (bool == null) {
  1587. t.multSelected = args;
  1588. } else {
  1589. let listId: number, sub: number;
  1590. if (bool) {
  1591. for (let n: number = args.length - 1; n >= 0; n--) {
  1592. listId = args[n];
  1593. sub = t.multSelected.indexOf(listId);
  1594. if (sub < 0) {
  1595. t.multSelected.push(listId);
  1596. }
  1597. }
  1598. } else {
  1599. for (let n: number = args.length - 1; n >= 0; n--) {
  1600. listId = args[n];
  1601. sub = t.multSelected.indexOf(listId);
  1602. if (sub >= 0) {
  1603. t.multSelected.splice(sub, 1);
  1604. }
  1605. }
  1606. }
  1607. }
  1608. t._forceUpdate = true;
  1609. t._onScrolling();
  1610. }
  1611. /**
  1612. * 获取多选数据
  1613. * @returns
  1614. */
  1615. getMultSelected() {
  1616. return this.multSelected;
  1617. }
  1618. /**
  1619. * 多选是否有选择
  1620. * @param {number} listId 索引
  1621. * @returns
  1622. */
  1623. hasMultSelected(listId: number) {
  1624. return this.multSelected && this.multSelected.indexOf(listId) >= 0;
  1625. }
  1626. /**
  1627. * 更新指定的Item
  1628. * @param {Array} args 单个listId,或者数组
  1629. * @returns
  1630. */
  1631. updateItem(args: any) {
  1632. if (!this.checkInited())
  1633. return;
  1634. if (!Array.isArray(args)) {
  1635. args = [args];
  1636. }
  1637. for (let n: number = 0, len: number = args.length; n < len; n++) {
  1638. let listId: number = args[n];
  1639. let item: any = this.getItemByListId(listId);
  1640. if (item)
  1641. EventHandler.emitEvents([this.renderEvent], item, listId % this._actualNumItems);
  1642. }
  1643. }
  1644. /**
  1645. * 更新全部
  1646. */
  1647. updateAll() {
  1648. if (!this.checkInited())
  1649. return;
  1650. this.numItems = this.numItems;
  1651. }
  1652. /**
  1653. * 根据ListID获取Item
  1654. * @param {Number} listId
  1655. * @returns
  1656. */
  1657. getItemByListId(listId: number) {
  1658. if (this.content) {
  1659. for (let n: number = this.content.children.length - 1; n >= 0; n--) {
  1660. let item: any = this.content.children[n];
  1661. if (item._listId == listId)
  1662. return item;
  1663. }
  1664. }
  1665. }
  1666. /**
  1667. * 获取在显示区域外的Item
  1668. * @returns
  1669. */
  1670. _getOutsideItem() {
  1671. let item: any;
  1672. let result: any[] = [];
  1673. for (let n: number = this.content.children.length - 1; n >= 0; n--) {
  1674. item = this.content.children[n];
  1675. if (!this.displayData.find(d => d.id == item._listId)) {
  1676. result.push(item);
  1677. }
  1678. }
  1679. return result;
  1680. }
  1681. //删除显示区域以外的Item
  1682. _delRedundantItem() {
  1683. if (this._virtual) {
  1684. let arr: any[] = this._getOutsideItem();
  1685. for (let n: number = arr.length - 1; n >= 0; n--) {
  1686. let item: any = arr[n];
  1687. if (this._scrollItem && item._listId == this._scrollItem._listId)
  1688. continue;
  1689. item.isCached = true;
  1690. PoolManager.putNode(item);
  1691. for (let m: number = this._lastDisplayData.length - 1; m >= 0; m--) {
  1692. if (this._lastDisplayData[m] == item._listId) {
  1693. this._lastDisplayData.splice(m, 1);
  1694. break;
  1695. }
  1696. }
  1697. }
  1698. // cc.log('存入::', str, ' pool.length =', this._pool.length);
  1699. } else {
  1700. while (this.content.children.length > this._numItems) {
  1701. this._delSingleItem(this.content.children[this.content.children.length - 1]);
  1702. }
  1703. }
  1704. }
  1705. //删除单个Item
  1706. _delSingleItem(item: any) {
  1707. // cc.log('DEL::', item['_listId'], item);
  1708. item.removeFromParent();
  1709. if (item.destroy)
  1710. item.destroy();
  1711. item = null;
  1712. }
  1713. /**
  1714. * 动效删除Item(此方法只适用于虚拟列表,即_virtual=true)
  1715. * 一定要在回调函数里重新设置新的numItems进行刷新,毕竟本List是靠数据驱动的。
  1716. */
  1717. aniDelItem(listId: number, callFunc: Function, aniType: number) {
  1718. let t: any = this;
  1719. if (!t.checkInited() || t.cyclic || !t._virtual)
  1720. return console.error('This function is not allowed to be called!');
  1721. if (!callFunc)
  1722. return console.error('CallFunc are not allowed to be NULL, You need to delete the corresponding index in the data array in the CallFunc!');
  1723. if (t._aniDelRuning)
  1724. return console.warn('Please wait for the current deletion to finish!');
  1725. let item: any = t.getItemByListId(listId);
  1726. let listItem: ListItem;
  1727. if (!item) {
  1728. callFunc(listId);
  1729. return;
  1730. } else {
  1731. listItem = item.getComponent(ListItem);
  1732. }
  1733. t._aniDelRuning = true;
  1734. t._aniDelCB = callFunc;
  1735. t._aniDelItem = item;
  1736. t._aniDelBeforePos = item.position;
  1737. t._aniDelBeforeScale = item.scale;
  1738. let curLastId: number = t.displayData[t.displayData.length - 1].id;
  1739. let resetSelectedId: boolean = listItem.selected;
  1740. listItem.showAni(aniType, () => {
  1741. //判断有没有下一个,如果有的话,创建粗来
  1742. let newId: number;
  1743. if (curLastId < t._numItems - 2) {
  1744. newId = curLastId + 1;
  1745. }
  1746. if (newId != null) {
  1747. let newData: any = t._calcItemPos(newId);
  1748. t.displayData.push(newData);
  1749. if (t._virtual)
  1750. t._createOrUpdateItem(newData);
  1751. else
  1752. t._createOrUpdateItem2(newId);
  1753. } else
  1754. t._numItems--;
  1755. if (t.selectedMode == SelectedType.SINGLE) {
  1756. if (resetSelectedId) {
  1757. t._selectedId = -1;
  1758. } else if (t._selectedId - 1 >= 0) {
  1759. t._selectedId--;
  1760. }
  1761. } else if (t.selectedMode == SelectedType.MULT && t.multSelected.length) {
  1762. let sub: number = t.multSelected.indexOf(listId);
  1763. if (sub >= 0) {
  1764. t.multSelected.splice(sub, 1);
  1765. }
  1766. //多选的数据,在其后的全部减一
  1767. for (let n: number = t.multSelected.length - 1; n >= 0; n--) {
  1768. let id: number = t.multSelected[n];
  1769. if (id >= listId)
  1770. t.multSelected[n]--;
  1771. }
  1772. }
  1773. if (t._customSize) {
  1774. if (t._customSize[listId])
  1775. delete t._customSize[listId];
  1776. let newCustomSize: any = {};
  1777. let size: number;
  1778. for (let id in t._customSize) {
  1779. size = t._customSize[id];
  1780. let idNumber: number = parseInt(id);
  1781. newCustomSize[idNumber - (idNumber >= listId ? 1 : 0)] = size;
  1782. }
  1783. t._customSize = newCustomSize;
  1784. }
  1785. //后面的Item向前怼的动效
  1786. let sec: number = .2333;
  1787. let twe: Tween<Node>, haveCB: boolean;
  1788. for (let n: number = newId != null ? newId : curLastId; n >= listId + 1; n--) {
  1789. item = t.getItemByListId(n);
  1790. if (item) {
  1791. let posData: any = t._calcItemPos(n - 1);
  1792. twe = tween(item)
  1793. .to(sec, { position: new Vec3(posData.x, posData.y, 0) });
  1794. if (n <= listId + 1) {
  1795. haveCB = true;
  1796. twe.call(() => {
  1797. t._aniDelRuning = false;
  1798. callFunc(listId);
  1799. delete t._aniDelCB;
  1800. });
  1801. }
  1802. twe.start();
  1803. }
  1804. }
  1805. if (!haveCB) {
  1806. t._aniDelRuning = false;
  1807. callFunc(listId);
  1808. t._aniDelCB = null;
  1809. }
  1810. }, true);
  1811. }
  1812. /**
  1813. * 滚动到..
  1814. * @param {Number} listId 索引(如果<0,则滚到首个Item位置,如果>=_numItems,则滚到最末Item位置)
  1815. * @param {Number} timeInSecond 时间
  1816. * @param {Number} offset 索引目标位置偏移,0-1
  1817. * @param {Boolean} overStress 滚动后是否强调该Item(这只是个实验功能)
  1818. */
  1819. scrollTo(listId: number, timeInSecond: number = .5, offset: number = null, overStress: boolean = false) {
  1820. let t = this;
  1821. if (!t.checkInited(false))
  1822. return;
  1823. // t._scrollView.stopAutoScroll();
  1824. if (timeInSecond == null) //默认0.5
  1825. timeInSecond = .5;
  1826. else if (timeInSecond < 0)
  1827. timeInSecond = 0;
  1828. if (listId < 0)
  1829. listId = 0;
  1830. else if (listId >= t._numItems)
  1831. listId = t._numItems - 1;
  1832. // 以防设置了numItems之后layout的尺寸还未更新
  1833. if (!t._virtual && t._layout && t._layout.enabled)
  1834. t._layout.updateLayout();
  1835. let pos = t.getItemPos(listId);
  1836. if (!pos) {
  1837. return DEV && console.error('pos is null', listId);
  1838. }
  1839. let targetX: number, targetY: number;
  1840. switch (t._alignCalcType) {
  1841. case 1://单行HORIZONTAL(LEFT_TO_RIGHT)、网格VERTICAL(LEFT_TO_RIGHT)
  1842. targetX = pos.left;
  1843. if (offset != null)
  1844. targetX -= t._thisNodeUt.width * offset;
  1845. else
  1846. targetX -= t._leftGap;
  1847. pos = new Vec3(targetX, 0, 0);
  1848. break;
  1849. case 2://单行HORIZONTAL(RIGHT_TO_LEFT)、网格VERTICAL(RIGHT_TO_LEFT)
  1850. targetX = pos.right - t._thisNodeUt.width;
  1851. if (offset != null)
  1852. targetX += t._thisNodeUt.width * offset;
  1853. else
  1854. targetX += t._rightGap;
  1855. pos = new Vec3(targetX + t._contentUt.width, 0, 0);
  1856. break;
  1857. case 3://单列VERTICAL(TOP_TO_BOTTOM)、网格HORIZONTAL(TOP_TO_BOTTOM)
  1858. targetY = pos.top;
  1859. if (offset != null)
  1860. targetY += t._thisNodeUt.height * offset;
  1861. else
  1862. targetY += t._topGap;
  1863. pos = new Vec3(0, -targetY, 0);
  1864. break;
  1865. case 4://单列VERTICAL(BOTTOM_TO_TOP)、网格HORIZONTAL(BOTTOM_TO_TOP)
  1866. targetY = pos.bottom + t._thisNodeUt.height;
  1867. if (offset != null)
  1868. targetY -= t._thisNodeUt.height * offset;
  1869. else
  1870. targetY -= t._bottomGap;
  1871. pos = new Vec3(0, -targetY + t._contentUt.height, 0);
  1872. break;
  1873. }
  1874. let viewPos: any = t.content.getPosition();
  1875. viewPos = Math.abs(t._sizeType ? viewPos.y : viewPos.x);
  1876. let comparePos = t._sizeType ? pos.y : pos.x;
  1877. let runScroll = Math.abs((t._scrollPos != null ? t._scrollPos : viewPos) - comparePos) > .5;
  1878. // cc.log(runScroll, t._scrollPos, viewPos, comparePos)
  1879. // t._scrollView.stopAutoScroll();
  1880. if (runScroll) {
  1881. t._scrollView.scrollToOffset(pos, timeInSecond);
  1882. t._scrollToListId = listId;
  1883. t._scrollToEndTime = ((new Date()).getTime() / 1000) + timeInSecond;
  1884. // cc.log(listId, t.content.width, t.content.getPosition(), pos);
  1885. t._scrollToSo = t.scheduleOnce(() => {
  1886. if (!t._adheringBarrier) {
  1887. t.adhering = t._adheringBarrier = false;
  1888. }
  1889. t._scrollPos =
  1890. t._scrollToListId =
  1891. t._scrollToEndTime =
  1892. t._scrollToSo =
  1893. null;
  1894. //cc.log('2222222222', t._adheringBarrier)
  1895. if (overStress) {
  1896. // t.scrollToListId = listId;
  1897. let item = t.getItemByListId(listId);
  1898. if (item) {
  1899. tween(item)
  1900. .to(.1, { scale: 1.05 })
  1901. .to(.1, { scale: 1 })
  1902. .start();
  1903. }
  1904. }
  1905. }, timeInSecond + .1);
  1906. if (timeInSecond <= 0) {
  1907. t._onScrolling();
  1908. }
  1909. }
  1910. }
  1911. /**
  1912. * 计算当前滚动窗最近的Item
  1913. */
  1914. _calcNearestItem() {
  1915. let t: any = this;
  1916. t.nearestListId = null;
  1917. let data: any, center: number;
  1918. if (t._virtual)
  1919. t._calcViewPos();
  1920. let vTop: number, vRight: number, vBottom: number, vLeft: number;
  1921. vTop = t.viewTop;
  1922. vRight = t.viewRight;
  1923. vBottom = t.viewBottom;
  1924. vLeft = t.viewLeft;
  1925. let breakFor: boolean = false;
  1926. for (let n = 0; n < t.content.children.length && !breakFor; n += t._colLineNum) {
  1927. data = t._virtual ? t.displayData[n] : t._calcExistItemPos(n);
  1928. if (data) {
  1929. center = t._sizeType ? ((data.top + data.bottom) / 2) : (center = (data.left + data.right) / 2);
  1930. switch (t._alignCalcType) {
  1931. case 1://单行HORIZONTAL(LEFT_TO_RIGHT)、网格VERTICAL(LEFT_TO_RIGHT)
  1932. if (data.right >= vLeft) {
  1933. t.nearestListId = data.id;
  1934. if (vLeft > center)
  1935. t.nearestListId += t._colLineNum;
  1936. breakFor = true;
  1937. }
  1938. break;
  1939. case 2://单行HORIZONTAL(RIGHT_TO_LEFT)、网格VERTICAL(RIGHT_TO_LEFT)
  1940. if (data.left <= vRight) {
  1941. t.nearestListId = data.id;
  1942. if (vRight < center)
  1943. t.nearestListId += t._colLineNum;
  1944. breakFor = true;
  1945. }
  1946. break;
  1947. case 3://单列VERTICAL(TOP_TO_BOTTOM)、网格HORIZONTAL(TOP_TO_BOTTOM)
  1948. if (data.bottom <= vTop) {
  1949. t.nearestListId = data.id;
  1950. if (vTop < center)
  1951. t.nearestListId += t._colLineNum;
  1952. breakFor = true;
  1953. }
  1954. break;
  1955. case 4://单列VERTICAL(BOTTOM_TO_TOP)、网格HORIZONTAL(BOTTOM_TO_TOP)
  1956. if (data.top >= vBottom) {
  1957. t.nearestListId = data.id;
  1958. if (vBottom > center)
  1959. t.nearestListId += t._colLineNum;
  1960. breakFor = true;
  1961. }
  1962. break;
  1963. }
  1964. }
  1965. }
  1966. //判断最后一个Item。。。(哎,这些判断真心恶心,判断了前面的还要判断最后一个。。。一开始呢,就只有一个布局(单列布局),那时候代码才三百行,后来就想着完善啊,艹..这坑真深,现在这行数都一千五了= =||)
  1967. data = t._virtual ? t.displayData[t.displayItemNum - 1] : t._calcExistItemPos(t._numItems - 1);
  1968. if (data && data.id == t._numItems - 1) {
  1969. center = t._sizeType ? ((data.top + data.bottom) / 2) : (center = (data.left + data.right) / 2);
  1970. switch (t._alignCalcType) {
  1971. case 1://单行HORIZONTAL(LEFT_TO_RIGHT)、网格VERTICAL(LEFT_TO_RIGHT)
  1972. if (vRight > center)
  1973. t.nearestListId = data.id;
  1974. break;
  1975. case 2://单行HORIZONTAL(RIGHT_TO_LEFT)、网格VERTICAL(RIGHT_TO_LEFT)
  1976. if (vLeft < center)
  1977. t.nearestListId = data.id;
  1978. break;
  1979. case 3://单列VERTICAL(TOP_TO_BOTTOM)、网格HORIZONTAL(TOP_TO_BOTTOM)
  1980. if (vBottom < center)
  1981. t.nearestListId = data.id;
  1982. break;
  1983. case 4://单列VERTICAL(BOTTOM_TO_TOP)、网格HORIZONTAL(BOTTOM_TO_TOP)
  1984. if (vTop > center)
  1985. t.nearestListId = data.id;
  1986. break;
  1987. }
  1988. }
  1989. // cc.log('t.nearestListId =', t.nearestListId);
  1990. }
  1991. //上一页
  1992. prePage(timeInSecond: number = .5) {
  1993. // cc.log('👈');
  1994. if (!this.checkInited())
  1995. return;
  1996. this.skipPage(this.curPageNum - 1, timeInSecond);
  1997. }
  1998. //下一页
  1999. nextPage(timeInSecond: number = .5) {
  2000. // cc.log('👉');
  2001. if (!this.checkInited())
  2002. return;
  2003. this.skipPage(this.curPageNum + 1, timeInSecond);
  2004. }
  2005. //跳转到第几页
  2006. skipPage(pageNum: number, timeInSecond: number) {
  2007. let t: any = this;
  2008. if (!t.checkInited())
  2009. return;
  2010. if (t._slideMode != SlideType.PAGE)
  2011. return console.error('This function is not allowed to be called, Must SlideMode = PAGE!');
  2012. if (pageNum < 0 || pageNum >= t._numItems)
  2013. return;
  2014. if (t.curPageNum == pageNum)
  2015. return;
  2016. // cc.log(pageNum);
  2017. t.curPageNum = pageNum;
  2018. if (t.pageChangeEvent) {
  2019. EventHandler.emitEvents([t.pageChangeEvent], pageNum);
  2020. }
  2021. t.scrollTo(pageNum, timeInSecond);
  2022. }
  2023. //计算 CustomSize(这个函数还是保留吧,某些罕见的情况的确还是需要手动计算customSize的)
  2024. calcCustomSize(numItems: number) {
  2025. let t: any = this;
  2026. if (!t.checkInited())
  2027. return;
  2028. if (!t._itemTmp)
  2029. return console.error('Unset template item!');
  2030. if (!t.renderEvent)
  2031. return console.error('Unset Render-Event!');
  2032. t._customSize = {};
  2033. let temp: any = PoolManager.getNode(t._itemTmp,t.content);
  2034. let ut: UITransform = temp.getComponent(UITransform);
  2035. for (let n: number = 0; n < numItems; n++) {
  2036. EventHandler.emitEvents([t.renderEvent], temp, n);
  2037. if (ut.height != t._itemSize.height || ut.width != t._itemSize.width) {
  2038. t._customSize[n] = t._sizeType ? ut.height : ut.width;
  2039. }
  2040. }
  2041. if (!Object.keys(t._customSize).length)
  2042. t._customSize = null;
  2043. temp.removeFromParent();
  2044. if (temp.destroy)
  2045. temp.destroy();
  2046. return t._customSize;
  2047. }
  2048. }