添加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

@@ -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() { }
});