-
-
Save mochiz/1950952 to your computer and use it in GitHub Desktop.
Faking Long Touch on Android in Titanium Mobile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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