Skip to content

Instantly share code, notes, and snippets.

@kwhinnery
Created June 10, 2011 15:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kwhinnery/1019105 to your computer and use it in GitHub Desktop.
Save kwhinnery/1019105 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);
row.add(Ti.UI.createLabel({
text:_title,
left:5,
textAlign:'left',
color:'#000',
font: {
fontWeight:'bold',
fontSize:14
}
}));
row.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();
@xa365
Copy link

xa365 commented Jun 19, 2011

What changes would be needed to support LongTouch for the entire row and not just on the graphic?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment