VirtualList.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. content:cc.Node,
  5. item:cc.Node,
  6. },
  7. // LIFE-CYCLE CALLBACKS:
  8. ctor:function(){
  9. this._disPlayItem = {};
  10. this._dataArr = [];
  11. this._cb = null;
  12. this._hook = null;
  13. this._keepCnt = 0;
  14. },
  15. onLoad () {
  16. this._pool = new cc.NodePool();
  17. this.item.active = false;
  18. this._view = this.node.getChildByName('view');
  19. this._viewWidth = this._view.getContentSize().width;
  20. this._viewHeight = this._view.getContentSize().height;
  21. this._viewTop = this._view.y + this._view.anchorY*this._viewHeight;
  22. this._viewBottom = this._view.y - (1-this._view.anchorY)*this._viewHeight;
  23. this._viewLeft = this._view.x - this._view.anchorX*this._viewWidth;
  24. this._viewRight = this._view.x + (1-this._view.anchorX)*this._viewWidth;
  25. this._scroll = this.getComponent(cc.ScrollView);
  26. },
  27. Init:function(keepCnt,cb,hook){
  28. while (this.content._children.length > keepCnt){
  29. let item = this.content._children[this.content._children.length-1];
  30. this._pool.put(item);
  31. }
  32. this._cb = cb;
  33. this._hook = hook;
  34. this._keepCnt = keepCnt;
  35. },
  36. InsertListData:function(data){
  37. this._dataArr.push(data);
  38. },
  39. ForEachCtrl:function(Call){
  40. for(var i in this.content._children){
  41. if(this._keepCnt>i) continue;
  42. let item = this.content._children[i];
  43. if(!item.active) continue;
  44. Call(item._children[0].getComponent(item._children[0].name));
  45. }
  46. },
  47. RecycleItem:function(item){
  48. this._pool.put(item);
  49. },
  50. GetAllItem:function(){
  51. return this.content._children;
  52. },
  53. update (dt) {
  54. if(this._dataArr.length <= 0) return;
  55. let data = this._dataArr.shift();
  56. let item = this._getItem();
  57. let js = item._children[0].getComponent(item._children[0].name);
  58. js.Init();
  59. js.m_Hook = this._hook;
  60. js.node.active = true;
  61. js.SetData(data);
  62. item.setContentSize(js.node.getContentSize());
  63. item.parent = this.content;
  64. if(this._cb) this._cb({data:data,item:item,js:js});
  65. this._filterDisplayItem();
  66. },
  67. onEnable:function(){
  68. this.node.on('scrolling',this._scrolling,this,true);
  69. },
  70. onDisable:function(){
  71. this.node.off('scrolling',this._scrolling,this,true);
  72. },
  73. destory:function(){
  74. this._pool.clear();
  75. },
  76. _scrolling:function(e){
  77. this._filterDisplayItem();
  78. },
  79. RefushList:function(){
  80. this.scheduleOnce(()=>{
  81. this._filterDisplayItem();
  82. },0.1);
  83. },
  84. _convertToViewPos:function(item){
  85. return this._view.convertToNodeSpaceAR(item.parent.convertToWorldSpaceAR(item.getPosition()));
  86. },
  87. _filterDisplayItem:function(){
  88. // let cs = this.content.getContentSize();
  89. // if(this._scroll.vertical){
  90. // if(cs.height <= this._viewHeight) return;
  91. // }else{
  92. // if(cs.width <= this._viewWidth) return;
  93. // }
  94. this._disPlayItem = {};
  95. for(var i in this.content._children){
  96. if(!this.content._children[i].active) continue;
  97. let item = this.content._children[i];
  98. let s = item.getContentSize();
  99. let p = this._convertToViewPos(item);
  100. let top = p.y+s.height + item.anchorY * s.height;
  101. let bottom = p.y-s.height + (1-item.anchorY) * s.height;
  102. let left = p.x-s.width + item.anchorX * s.width;
  103. let right = p.x+s.width + (1-item.anchorX) * s.width;
  104. if(this._scroll.vertical){
  105. if(bottom <= this._viewTop && top>=this._viewBottom){
  106. this._disPlayItem[i] = item;
  107. }
  108. }else{
  109. if(right >= this._viewLeft && left<=this._viewRight){
  110. this._disPlayItem[i] = item;
  111. }
  112. }
  113. }
  114. this._disPlayShow();
  115. },
  116. _disPlayShow:function(){
  117. for(var i in this.content._children){
  118. if(i == 0) continue
  119. this.content._children[i]._children[0].active = !!this._disPlayItem[i];
  120. }
  121. },
  122. _getItem:function(){
  123. let item = this._pool.get();
  124. if(item == null){
  125. let t = cc.instantiate(this.item);
  126. t.x = 0;
  127. t.y = 0;
  128. //let s = t.getContentSize();
  129. item = new cc.Node();
  130. t.parent = item;
  131. //item.setContentSize(s);
  132. }
  133. return item;
  134. },
  135. });