我在应用加速器 Titanium 中有疑问:如何EventListener
动态概括?在我的项目中,我有一个listSection
动态创建的数组。问题是,我如何创建一个事件侦听器,以允许整体listView
共享相同的事件行为?
var sections = [];
var SectionOne = Ti.UI.createListSection({
headerTitle : "Section one"
});
var SectionTwo = Ti.UI.createListSection({
headerTitle : "Section two"
});
var SectionThree = Ti.UI.createListSection({
headerTitle : "Section three"
});
sections.push(SectionOne);
sections.push(SectionTwo);
sections.push(SectionThree);
/*problem below*/
listView.addEventListener('itemclick', function(e) {
for (var sec in sections) {
var item = sections[sec].getItemAt(e.itemIndex);
if (item.properties.accessoryType == Ti.UI.LIST_ACCESSORY_TYPE_NONE) {
audioPlayer.start();
item.properties.accessoryType = Ti.UI.LIST_ACCESSORY_TYPE_CHECKMARK;
item.properties.color = 'red';
Ti.API.info('e.index: ' + e.itemIndex);
} else {
audioPlayer.stop();
item.properties.accessoryType = Ti.UI.LIST_ACCESSORY_TYPE_NONE;
item.properties.color = 'black';
}
sections[sec].updateItemAt(e.itemIndex, item);
}
});
win.add(listView);