List.ts 87 KB

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