userListInfo.js 4.5 KB

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