mirror of
https://github.com/Leo501/CocosCreatorTutorial.git
synced 2026-09-03 07:27:12 +08:00
添加listView中addData的动态加数据
This commit is contained in:
@@ -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
@@ -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();
|
||||
},
|
||||
});
|
||||
@@ -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;
|
||||
@@ -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() { }
|
||||
});
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"ver": "1.0.1",
|
||||
"uuid": "d6a6c9a3-1d26-4236-8c6e-26cbf483984b",
|
||||
"isSubpackage": false,
|
||||
"subpackageName": "",
|
||||
"subMetas": {}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user