Commit 3942b415 authored by Li MingZhe's avatar Li MingZhe

feat: 底部item 宽度适配

parent d896137d
......@@ -22,6 +22,7 @@ cc.Class({
_maxPage: null,
_lineMaxCount: null,
_disW: null,
_maxW: null,
onLoad() {
this._canTouch = true;
this._isTop = false;
......@@ -35,8 +36,8 @@ cc.Class({
this._maxPage = 0;
this._disW = 5;
const maxW = 1280 - 90;
this._lineMaxCount = Math.floor(maxW / (this._disW + this._itemLen));
this._maxW = 1280 - 90;
this._lineMaxCount = Math.floor(this._maxW / (this._disW + this._itemLen));
},
......@@ -112,7 +113,7 @@ cc.Class({
return;
}
if (this._pageIndex >= this._maxPage) {
if (this._pageIndex >= this._maxPage - 1) {
return;
}
this._pageIndex++;
......@@ -142,7 +143,7 @@ cc.Class({
this._pageUpBtn.opacity = 255;
}
if (this._pageIndex == this._maxPage) {
if (this._pageIndex == this._maxPage - 1) {
this._pageDownBtn.opacity = 100;
} else {
this._pageDownBtn.opacity = 255;
......@@ -185,12 +186,35 @@ cc.Class({
return;
}
this._dataArr = dataArr;
this._maxPage = Math.ceil(dataArr.length / this._lineMaxCount) - 1;
// this._maxPage = Math.ceil(dataArr.length / this._lineMaxCount) - 1;
this._initPageArr();
this._pageIndex = 0;
this._refreshPage();
},
_pageArr: null,
_initPageArr() {
let tmpArr = [];
this._pageArr = [tmpArr];
let curLen = this._disW;
for (let i=0; i<this._dataArr.length; i++) {
const item = this._addOneItem(this._dataArr[i]);
curLen += item.width + this._disW;
if (curLen > this._maxW) {
tmpArr = [item];
this._pageArr.push(tmpArr);
} else {
tmpArr.push(item);
}
}
this._maxPage = this._pageArr.length;
console.log('this._maxPage: ', this._maxPage);
},
_itemLen: null,
_addOneItem(data) {
......@@ -207,6 +231,7 @@ cc.Class({
this._addItemLabel(item, data.text);
this._addItemAudio(item, data.audio_url);
item.data = data;
return item;
},
......@@ -254,6 +279,10 @@ cc.Class({
}
label.font = this._labelFont;
label._forceUpdateRenderData(true)
if (labelNode.width > this._itemLen) {
item.width = labelNode.width;
}
},
......@@ -286,20 +315,35 @@ cc.Class({
const disW = this._disW;
const startIndex = this._pageIndex * this._lineMaxCount;
const showArr = dataArr.slice(startIndex, startIndex + this._lineMaxCount);
// const showArr = dataArr.slice(startIndex, startIndex + this._lineMaxCount);
const showArr = this._pageArr[this._pageIndex];
let baseX = this._getCurPageBaseX();
const baseX = -(disW + this._itemLen) * (showArr.length - 1) / 2;
// const baseX = -(disW + this._itemLen) * (showArr.length - 1) / 2;
const baseY = -25;
showArr.forEach((data, i) => {
const item = this._addOneItem(data);
showArr.forEach((el, i) => {
const item = this._addOneItem(el.data);
this._itemLayer.addChild(item);
item.x = baseX + (disW + this._itemLen) * i;
item.x = baseX + (disW + item.width) / 2
item.y = baseY;
baseX = item.x + (disW + item.width) / 2;
});
this._setPageBtnState();
},
_getCurPageBaseX() {
const arr = this._pageArr[this._pageIndex];
console.log('arr: ', arr);
let curLen = this._disW;
for (let i=0; i<arr.length; i++) {
curLen += (arr[i].width + this._disW)
}
return -curLen / 2;
},
// update (dt) {},
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment