試試這個commonJS模塊,並根據您的心願進行定製,您也可以將它簡化爲您自己的使用案例。此係統適用於iOS的iOS & Android,但以下示例適用於iOS(由於NavWindow)。
app.js:
var win = Ti.UI.createWindow({backgroundColor:'white', title:'SWIPEABLE CELL'})
var navWin = Ti.UI.iOS.createNavigationWindow({
window:win
})
function genWindow(module) {
var ListTest = require(module);
var win = new ListTest();
return win;
}
var b1 = Ti.UI.createButton({title:'LIST VIEW SAMPLE', bottom:100});
win.add(b1);
b1.addEventListener('click',function(e){
navWin.openWindow(genWindow('listviewsample'));
});
navWin.open();
listviewsample.js:
function leftSwipeHandler(e) {
if (e.direction == 'left') {
var theItem = e.section.getItemAt(e.itemIndex);
theItem.template = 't2';
e.section.updateItemAt(e.itemIndex,theItem,{animated:true,animationStyle:Titanium.UI.iPhone.RowAnimationStyle.LEFT});
}
}
function rightSwipeHandler(e) {
if (e.direction == 'right') {
var theItem = e.section.getItemAt(e.itemIndex);
theItem.template = 't1';
e.section.updateItemAt(e.itemIndex,theItem,{animated:true,animationStyle:Titanium.UI.iPhone.RowAnimationStyle.RIGHT});
}
}
function trashHandler(e) {
e.section.deleteItemsAt(e.itemIndex,1,{animated:true,animationStyle:Titanium.UI.iPhone.RowAnimationStyle.FADE});
}
function doneHandler(e) {
var theItem = e.section.getItemAt(e.itemIndex);
theItem.template = 't3';
e.section.updateItemAt(e.itemIndex,theItem,{animated:true,animationStyle:Titanium.UI.iPhone.RowAnimationStyle.FADE});
}
var baseTemplate = {
properties:{height:50},
childTemplates: [
{
type: 'Ti.UI.View',
events:{
swipe:leftSwipeHandler
},
childTemplates:[
{
type:'Ti.UI.Label',
bindId:'content',
properties:{left:10,touchEnabled:false,color:'black'}
}
]
}
]
};
var controlTemplate = {
properties:{height:50},
childTemplates: [
{
type: 'Ti.UI.View',
properties:{left:-150,width:'100%'},
events:{
swipe:rightSwipeHandler
},
childTemplates:[
{
type:'Ti.UI.Label',
bindId:'content',
properties:{left:10,touchEnabled:false,color:'black'}
}
]
},
{
type:'Ti.UI.Label',
properties:{color:'white',text:'Trash',width:75,right:75, height:50, backgroundColor:'red', textAlign:Ti.UI.TEXT_ALIGNMENT_CENTER},
events:{
click:trashHandler
}
},
{
type:'Ti.UI.Label',
properties:{color:'white',text:'Finish',width:75,right:0, height:50, backgroundColor:'green', textAlign:Ti.UI.TEXT_ALIGNMENT_CENTER},
events:{
click:doneHandler
}
}
]
}
var doneTemplate = {
properties:{height:50},
childTemplates: [
{
type: 'Ti.UI.View',
childTemplates:[
{
type:'Ti.UI.Label',
bindId:'content',
properties:{left:10,touchEnabled:false, color:'green'}
}
]
}
]
};
var data = [];
for(var i=0;i<20;i++) {
data.push({properties:{backgroundColor:'white',selectionStyle:Ti.UI.iPhone.ListViewCellSelectionStyle.NONE},content:{text:'THIS IS A TASK INDEX'+(i+1)}})
}
var section = Ti.UI.createListSection({items:data});
var listView = Ti.UI.createListView({
sections:[section],
templates:{'t1':baseTemplate,'t2':controlTemplate,'t3':doneTemplate},
defaultItemTemplate:'t1'
})
function listviewsample() {
var win = Ti.UI.createWindow({backgroundColor:'white', title:'LISTVIEW', backButtonTitle:'BACK'});
win.add(listView);
return win;
}
module.exports = listviewsample;
你可否透露更多關於如何使用列表視圖,我想上面的代碼來實現,但它不工作 –