Skip to content

Instantly share code, notes, and snippets.

@mochiz
Forked from oroce/app.js
Created March 1, 2012 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mochiz/1950952 to your computer and use it in GitHub Desktop.
Save mochiz/1950952 to your computer and use it in GitHub Desktop.
Faking Long Touch on Android in Titanium Mobile
//The table view row just has to have a full height view in order
//to trigger the TableView touchstart
function LongTouchTableViewRow(_title) {
var row = Ti.UI.createTableViewRow({
height:50
});
var v = Ti.UI.createView({
height:50
});
row.add(v);
//that's how this works for me
//we need to add label to view, not to the row
v.add(Ti.UI.createLabel({
text:_title,
left:5,
textAlign:'left',
color:'#000',
font: {
fontWeight:'bold',
fontSize:14
}
}));
//same reason as label
v.add(Ti.UI.createImageView({
image:Ti.Android.R.drawable.ic_menu_more,
right:5,
height:'auto',
width:'auto'
}));
return row;
}
function LongTouchTableView(_options) {
var tv = Ti.UI.createTableView(_options),
touched = false,
interval = _options.interval||1000;
//Touchstart/touchend manages the press
tv.addEventListener('touchstart', function(e) {
touched = true;
setTimeout(function() {
if (touched) {
tv.fireEvent('longTouch');
}
},interval);
});
tv.addEventListener('touchmove', function(e) {
touched = false;
});
tv.addEventListener('touchend', function(e) {
touched = false;
});
return tv;
}
//Build test UI
var data = [];
for (var i = 0;i<30;i++) {
data.push(new LongTouchTableViewRow('Item '+i));
}
var tv = new LongTouchTableView({
data:data
});
tv.addEventListener('longTouch', function(e) {
alert('long touched!');
});
var win = Titanium.UI.createWindow({
backgroundColor:'#fff'
});
win.add(tv);
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment