userListInfo.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. _infoScript.setInfo({item:_item});
  68. }
  69. },
  70. load_scroll_recode: function () {
  71. //向下加载数据
  72. //当开始位置比value_set的长度小则代表没加载完
  73. if (this.start_index + this.PAGE_NUM * 3 < this.value_set.length &&
  74. this.listContainer.y >= this.start_y + this.PAGE_NUM * 2 * this.HIGH)//content超过2个PAGE的高度
  75. {
  76. //_autoScrolling在引擎源码中负责处理scrollview的滚动动作
  77. if (this.scroll_view._autoScrolling) { //等自动滚动结束后再加载防止滚动过快,直接跳到非常后的位置
  78. this.scroll_view.elastic = false; //关闭回弹效果 美观
  79. return;
  80. }
  81. var down_loaded = this.PAGE_NUM;
  82. this.start_index += down_loaded;
  83. if (this.start_index + this.PAGE_NUM * 3 > this.value_set.length) {
  84. //超过数据范围的长度
  85. var out_len = this.start_index + this.PAGE_NUM * 3 - this.value_set.length;
  86. down_loaded -= out_len;
  87. this.start_index -= out_len;
  88. }
  89. this.load_recode(this.start_index);
  90. this.listContainer.y -= down_loaded * this.HIGH;
  91. return;
  92. }
  93. //向上加载
  94. if (this.start_index > 0 && this.listContainer.y <= this.start_y) {
  95. if (this.scroll_view._autoScrolling) {
  96. this.scroll_view.elastic = false;
  97. return;
  98. }
  99. var up_loaded = this.PAGE_NUM;
  100. this.start_index -= up_loaded;
  101. if (this.start_index < 0) {
  102. up_loaded += this.start_index;
  103. this.start_index = 0;
  104. }
  105. this.load_recode(this.start_index);
  106. this.listContainer.y += up_loaded * this.HIGH;
  107. }
  108. },
  109. on_scroll_ended: function () {
  110. this.load_scroll_recode();
  111. this.scroll_view.elastic = true; //加载结束后自动滚动回弹开启
  112. },
  113. update(dt) {
  114. // this.load_scroll_recode();
  115. },
  116. /**
  117. * 显示偷菜可偷列表
  118. */
  119. onOpen() {
  120. this.node.active = true;
  121. },
  122. onClose() {
  123. this.node.active = false;
  124. },
  125. });