userListInfo.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * 可偷菜的用户信息
  3. */
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. listInfoPrefab: {
  8. default: null,
  9. type: cc.Prefab,
  10. serializable: true,
  11. },
  12. listContainer: {
  13. default: null,
  14. type: cc.Node
  15. },
  16. value_set: {
  17. default: null,
  18. visible: false
  19. },
  20. scroll_view: { //获取scrollview组件
  21. type: cc.ScrollView,
  22. default: null,
  23. },
  24. HIGH: 80, //每一项的高度
  25. PAGE_NUM: 4, //每一页4个项
  26. opt_item_set: [],
  27. //是否在刷新列表
  28. bUpdateList:false,
  29. },
  30. // LIFE-CYCLE CALLBACKS:
  31. onLoad() {
  32. },
  33. start() {
  34. // this.onOpentList();
  35. this.scroll_view.node.on("scroll-ended", this.on_scroll_ended.bind(this), this);//监听scrollview事件
  36. this.opt_item_set = [];
  37. //每次加载3页
  38. for (var i = 0; i < this.PAGE_NUM * 3; i++) {
  39. let _info = cc.instantiate(this.listInfoPrefab);
  40. _info.active = false;
  41. this.listContainer.addChild(_info);
  42. this.opt_item_set.push(_info);
  43. }
  44. this.start_y = this.listContainer.y;//初始化起始y坐标
  45. this.start_index = 0; //100项数据里面的起始数据记录索引
  46. // this.load_recode(this.start_index);
  47. this.loadList();
  48. },
  49. //显示列表
  50. onShowUserList(list) {
  51. this.value_set = Object.assign([], list);
  52. this.load_recode(this.start_index);
  53. },
  54. load_recode: function (start_index) {
  55. this.start_index = start_index;
  56. if (this.opt_item_set.length <= 0) return;
  57. for (var i = 0; i < this.PAGE_NUM * 3; i++) {
  58. if (this.value_set.length <= i) {
  59. this.opt_item_set[i].active = false;
  60. continue;
  61. }
  62. this.opt_item_set[i].active = true;
  63. //处理item
  64. let _infoScript = this.opt_item_set[i].getComponent("userListInfoItem");
  65. let _item = this.value_set[i];
  66. _infoScript.setInfo({item:_item});
  67. }
  68. },
  69. load_scroll_recode: function () {
  70. //向下加载数据
  71. //当开始位置比value_set的长度小则代表没加载完
  72. if (this.start_index + this.PAGE_NUM * 3 < this.value_set.length &&
  73. this.listContainer.y >= this.start_y + this.PAGE_NUM * 2 * this.HIGH)//content超过2个PAGE的高度
  74. {
  75. //_autoScrolling在引擎源码中负责处理scrollview的滚动动作
  76. if (this.scroll_view._autoScrolling) { //等自动滚动结束后再加载防止滚动过快,直接跳到非常后的位置
  77. this.scroll_view.elastic = false; //关闭回弹效果 美观
  78. return;
  79. }
  80. var down_loaded = this.PAGE_NUM;
  81. this.start_index += down_loaded;
  82. if (this.start_index + this.PAGE_NUM * 3 > this.value_set.length) {
  83. //超过数据范围的长度
  84. var out_len = this.start_index + this.PAGE_NUM * 3 - this.value_set.length;
  85. down_loaded -= out_len;
  86. this.start_index -= out_len;
  87. }
  88. this.load_recode(this.start_index);
  89. this.listContainer.y -= down_loaded * this.HIGH;
  90. return;
  91. }
  92. //向上加载
  93. if (this.start_index > 0 && this.listContainer.y <= this.start_y) {
  94. if (this.scroll_view._autoScrolling) {
  95. this.scroll_view.elastic = false;
  96. return;
  97. }
  98. var up_loaded = this.PAGE_NUM;
  99. this.start_index -= up_loaded;
  100. if (this.start_index < 0) {
  101. up_loaded += this.start_index;
  102. this.start_index = 0;
  103. }
  104. this.load_recode(this.start_index);
  105. this.listContainer.y += up_loaded * this.HIGH;
  106. }
  107. },
  108. on_scroll_ended: function () {
  109. this.load_scroll_recode();
  110. this.scroll_view.elastic = true; //加载结束后自动滚动回弹开启
  111. },
  112. // update(dt) {
  113. // this.load_scroll_recode();
  114. // },
  115. /**
  116. * 显示偷菜可偷列表
  117. */
  118. onOpen() {
  119. this.node.active = true;
  120. },
  121. onClose() {
  122. this.node.active = false;
  123. },
  124. //刷新列表
  125. onRefreshEvent(){
  126. this.loadList();
  127. },
  128. loadList(){
  129. if(this.bUpdateList)return;
  130. this.bUpdateList = true;
  131. GlobalD.GameData.onGetCanStealUserList(0, 10, (list) => {
  132. console.log("==========", list);
  133. this.bUpdateList = false;
  134. this.onShowUserList(list);
  135. });
  136. }
  137. });