ListViewCtrl.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. itemTemplate: { // item template to instantiate other items
  5. default: null,
  6. type: cc.Node
  7. },
  8. scrollView: {
  9. default: null,
  10. type: cc.ScrollView
  11. },
  12. spawnCount: 0, // how many items we actually spawn
  13. totalCount: 0, // how many items we need for the whole list
  14. spacing: 0, // space between each item
  15. bufferZone: 0, // when item is away from bufferZone, we relocate it
  16. lblScrollEvent: cc.Label,
  17. btnAddItem: cc.Button,
  18. btnRemoveItem: cc.Button,
  19. btnJumpToPosition: cc.Button,
  20. lblJumpPosition: cc.Label,
  21. lblTotalItems: cc.Label,
  22. totalRankUrl:"",
  23. timeline:"0",
  24. count:10,
  25. // item:[],
  26. ver:100,
  27. os:1,
  28. channel:"wexin",
  29. hasNext:true,
  30. },
  31. // use this for initialization
  32. onLoad: function () {
  33. this.content = this.scrollView.content;
  34. // console.log(this.items);
  35. this.updateTimer = 0;
  36. this.updateInterval = 0.2;
  37. this.lastContentPosY = 0; // use this variable to detect if we are scrolling up or down
  38. },
  39. onEnable:function(){
  40. if(this.items!=undefined){
  41. this.items.clear();
  42. }else{
  43. this.items = [];
  44. }
  45. this.getSystemInfo();
  46. },
  47. getUserInfo:function(){
  48. wx.getStorage({
  49. key:"userInfo",
  50. success:(res)=>{
  51. this.getTotalRank(res.data);
  52. }
  53. })
  54. },
  55. getSystemInfo:function(){
  56. if(wx!=undefined){
  57. wx.getSystemInfo({
  58. success:(res)=>{
  59. this.os = res.platform=="android"?2:1;
  60. this.getUserInfo();
  61. }
  62. })
  63. }
  64. },
  65. getTotalRank:function(userInfo){
  66. console.log(userInfo.token)
  67. console.log(userInfo.uid)
  68. if(wx!=undefined){
  69. var self = this;
  70. console.log("去网络数据");
  71. if(this.hasNext){
  72. wx.request({
  73. url:self.totalRankUrl,
  74. data:{
  75. token:userInfo.token,
  76. uid:userInfo.uid,
  77. channel:self.channel,
  78. os:self.os,
  79. ver:self.ver,
  80. timeline:self.timeline,
  81. count:self.count
  82. },
  83. success:(response)=>{
  84. console.log(response);
  85. var data =response.data.data;
  86. if(data!=undefined){
  87. console.log(this.users);
  88.  console.log(data);
  89. this.timeline = data.timeline;
  90. this.hasNext = data.next>0;
  91. this.users = data.users;
  92. // this.users.concat(data.users);
  93. this.initialize(data.users);
  94. }
  95. },
  96. fail:()=>{
  97. wx.showToast({
  98. title:"网络请求出错",
  99. })
  100. }
  101. })
  102. }else{
  103. wx.showToast({
  104. title:"没有更多了~"
  105. })
  106. }
  107. }
  108. },
  109. initialize: function () {
  110. this.content.height = this.totalCount * (this.itemTemplate.height + this.spacing) + this.spacing; // get total content height
  111. for (let i = 0; i < this.spawnCount; ++i) { // spawn items, we only need to do this once
  112. let item = cc.instantiate(this.itemTemplate);
  113. this.content.addChild(item);
  114. item.setPosition(0, -item.height * (0.5 + i) - this.spacing * (i + 1));
  115. item.getComponent('Item').updateItem(this.users);
  116. this.items.push(item);
  117. }
  118. },
  119. getPositionInView: function (item) { // get item position in scrollview's node space
  120. let worldPos = item.parent.convertToWorldSpaceAR(item.position);
  121. let viewPos = this.scrollView.node.convertToNodeSpaceAR(worldPos);
  122. return viewPos;
  123. },
  124. update: function(dt) {
  125. this.updateTimer += dt;
  126. if (this.updateTimer < this.updateInterval) return; // we don't need to do the math every frame
  127. this.updateTimer = 0;
  128. let items = this.items;
  129. let buffer = this.bufferZone;
  130. let isDown = this.scrollView.content.y < this.lastContentPosY; // scrolling direction
  131. let offset = (this.itemTemplate.height + this.spacing) * items.length;
  132. for (let i = 0; i < items.length; ++i) {
  133. let viewPos = this.getPositionInView(items[i]);
  134. if (isDown) {
  135. // if away from buffer zone and not reaching top of content
  136. if (viewPos.y < -buffer && items[i].y + offset < 0) {
  137. items[i].setPositionY(items[i].y + offset );
  138. let item = items[i].getComponent('Item');
  139. item.updateItem(this.users[i]);
  140. }
  141. } else {
  142. // if away from buffer zone and not reaching bottom of content
  143. if (viewPos.y > buffer && items[i].y - offset > -this.content.height) {
  144. items[i].setPositionY(items[i].y - offset );
  145. let item = items[i].getComponent('Item');
  146. item.updateItem(this.users[i]);
  147. }
  148. }
  149. }
  150. // update lastContentPosY
  151. this.lastContentPosY = this.scrollView.content.y;
  152. // this.lblTotalItems.textKey = "Total Items: " + this.totalCount;
  153. },
  154. scrollEvent: function(sender, event) {
  155. switch(event) {
  156. case 0:
  157. this.lblScrollEvent.string = "Scroll to Top";
  158. break;
  159. case 1:
  160. this.lblScrollEvent.string = "Scroll to Bottom";
  161. break;
  162. case 2:
  163. this.lblScrollEvent.string = "Scroll to Left";
  164. break;
  165. case 3:
  166. this.lblScrollEvent.string = "Scroll to Right";
  167. break;
  168. case 4:
  169. this.lblScrollEvent.string = "Scrolling";
  170. break;
  171. case 5:
  172. this.lblScrollEvent.string = "Bounce Top";
  173. break;
  174. case 6:
  175. this.lblScrollEvent.string = "Bounce bottom";
  176. break;
  177. case 7:
  178. this.lblScrollEvent.string = "Bounce left";
  179. break;
  180. case 8:
  181. this.lblScrollEvent.string = "Bounce right";
  182. break;
  183. case 9:
  184. this.lblScrollEvent.string = "Auto scroll ended";
  185. break;
  186. }
  187. },
  188. addItem: function() {
  189. this.content.height = (this.totalCount + 1) * (this.itemTemplate.height + this.spacing) + this.spacing; // get total content height
  190. this.totalCount = this.totalCount + 1;
  191. },
  192. removeItem: function() {
  193. if (this.totalCount - 1 < 30) {
  194. cc.error("can't remove item less than 30!");
  195. return;
  196. }
  197. this.content.height = (this.totalCount - 1) * (this.itemTemplate.height + this.spacing) + this.spacing; // get total content height
  198. this.totalCount = this.totalCount - 1;
  199. },
  200. scrollToFixedPosition: function () {
  201. this.scrollView.scrollToOffset(cc.p(0, 500), 2);
  202. }
  203. });