添加listView中addData的动态加数据

This commit is contained in:
leo
2019-02-01 20:16:23 +08:00
parent a895e570e3
commit d5b006beb7
9 changed files with 698 additions and 279 deletions

View File

@@ -1,6 +1,7 @@
{
"ver": "1.0.0",
"uuid": "ee8a03a3-1f23-464b-abf1-1800df851e7e",
"optimizationPolicy": "AUTO",
"asyncLoadAssets": false,
"subMetas": {}
}

File diff suppressed because it is too large Load Diff

View File

@@ -16,8 +16,14 @@ cc.Class({
},
start() {
const adapter = new ListAdapter();
adapter.setDataSet([1, 2, 3, 4, 5, 6, 7, 8, 89, 9, 12, 1243, 45, 564, 6756, 876, 7988, 789, 78987, 978, 45, 6732, 423, 42], 'ListItem');
this.listView.setAdapter(adapter);
}
this.adapter = new ListAdapter();
this.adapter.setDataSet([1, 2, 3, 4, 5, 6, 7, 8, 89, 9, 12, 1243, 45, 564, 6756, 876, 7988, 789, 78987, 978, 45, 6732, 423, 42], 'ListItem');
this.listView.setAdapter(this.adapter);
},
onBtnAdd() {
let data = [321, 322, 323, 324];
this.adapter.addData(data);
this.listView.resetScrollLength();
},
});

View File

@@ -1,39 +1,44 @@
class AbsAdapter {
constructor() {
this.dataSet = [];
this.componentName = '';
}
/**
*
* @param {*} data 数据
* @param {*} componentName item的脚本名
*/
setDataSet(data = [], componentName) {
this.dataSet = data;
this.componentName = componentName;
}
getCount() {
return this.dataSet.length;
}
getItem(posIndex) {
return this.dataSet[posIndex];
}
_getView(item, posIndex) {
this.updateView(item, posIndex);
return item;
}
updateView(item, posIndex) {
let comp = item.getComponent(this.componentName);
if (comp) {
comp.setData(this.getItem(posIndex));
}
constructor() {
this.dataSet = [];
this.componentName = '';
}
/**
*
* @param {*} data 数据
* @param {*} componentName item的脚本名
*/
setDataSet(data = [], componentName) {
this.dataSet = data;
this.componentName = componentName;
}
addData(data) {
console.log('addData');
this.dataSet.push.apply(this.dataSet, data);
}
getCount() {
return this.dataSet.length;
}
getItem(posIndex) {
return this.dataSet[posIndex];
}
_getView(item, posIndex) {
this.updateView(item, posIndex);
return item;
}
updateView(item, posIndex) {
let comp = item.getComponent(this.componentName);
if (comp) {
comp.setData(this.getItem(posIndex));
}
}
module.exports = AbsAdapter;
}
module.exports = AbsAdapter;

View File

@@ -3,8 +3,8 @@ cc.Class({
properties: {
itemTemplate: cc.Prefab,
spacing: 1,
spawnCount: 2,
spacing: 1,//每个itme的间隔
spawnCount: 2,//预加载item个数
scrollView: cc.ScrollView,
},
@@ -22,8 +22,8 @@ cc.Class({
this.lastStartIndex = -1;
this.scrollTopNotifyed = false;
this.scrollBottomNotifyed = false;
this.pullDownCallback = () => {};
this.pullUpCallback = () => {};
this.pullDownCallback = () => { };
this.pullUpCallback = () => { };
},
onLoad() {
@@ -117,14 +117,19 @@ cc.Class({
});
}
this.lastStartIndex = -1;
this.resetScrollLength();
this.scrollView.scrollToTop();
},
resetScrollLength() {
if (this.horizontal) {
this.content.width = this.adapter.getCount() * (this._itemWidth + this.spacing) + this.spacing;
} else {
this.content.height = this.adapter.getCount() * (this._itemHeight + this.spacing) + this.spacing; // get total content height
}
this.scrollView.scrollToTop();
},
scrollToTop(anim = false) {
this.scrollView.scrollToTop(anim ? 1 : 0);
},
@@ -320,5 +325,5 @@ cc.Class({
return (cc.sys.isMobile || cc.sys.platform === cc.sys.WECHAT_GAME || cc.sys.platform === cc.sys.QQ_PLAY);
},
onDestroy() {}
onDestroy() { }
});

View File

@@ -1,9 +1,10 @@
{
"ver": "1.0.0",
"ver": "2.2.0",
"uuid": "6aa0aa6a-ebee-4155-a088-a687a6aadec4",
"type": "sprite",
"wrapMode": "clamp",
"filterMode": "bilinear",
"premultiplyAlpha": false,
"subMetas": {
"HelloWorld": {
"ver": "1.0.3",

View File

@@ -1,9 +1,10 @@
{
"ver": "1.0.0",
"ver": "2.2.0",
"uuid": "a8027877-d8d6-4645-97a0-52d4a0123dba",
"type": "sprite",
"wrapMode": "clamp",
"filterMode": "bilinear",
"premultiplyAlpha": false,
"subMetas": {
"singleColor": {
"ver": "1.0.3",

View File

@@ -1,7 +0,0 @@
{
"ver": "1.0.1",
"uuid": "d6a6c9a3-1d26-4236-8c6e-26cbf483984b",
"isSubpackage": false,
"subpackageName": "",
"subMetas": {}
}

View File

@@ -24,5 +24,16 @@
"enable": false,
"appID": "13798",
"appSecret": "959b3ac0037d0f3c2fdce94f8421a9b2"
},
"last-module-event-record-time": 0,
"facebook": {
"enable": false,
"appID": "",
"live": {
"enable": false
},
"audience": {
"enable": false
}
}
}