Long press on android, or simulating it with touchstart/end
I'm trying to get long presses to work with table rows on android. Apparently it's not supported with current Titanium releases. Following some samples posted in other Q&As, I came up with the following, but the "touchstart fired" never shows up in the console. So, apparently that event isn't working either. Suggestions?
var holdTime = 1000;
var startTime;
profileTable.addEventListener('touchstart', function(e) {
Ti.API.info("touchstart fired");
startTime = new Date();
});
profileTable.addEventListener('touchend', function(e) {
Ti.API.info("touchend fired");
var now = new Date();
if(now - startTime > holdTime) {
alert(e.rowData.title);
} else {
alert(e.rowData.profileID);
}
startTime = '';
});
4 Answers
-
Accepted Answer
The best way I've been able to simulate a long press on a table view is by the following:
var touched = false; tableview.addEventListener('click', function(e) { touched = false; }); tableview.addEventListener('touchstart', function(e){ touched = true; setTimeout(function(){ if (touched) alert("Long press"); },1000); }); tableview.addEventListener('touchmove', function(e){ touched = false; }); tableview.addEventListener('touchend', function(e){ touched = false; });
Instead of an alert you can use Titanium.UI.OptionDialog to display options.
-
Try the code below:
var tStart; profileTable.addEventListener('touchstart', function(e) { Ti.API.info("touchstart fired"); tStart = new Date(); }); profileTable.addEventListener('touchend', function(e) { Ti.API.info("touchend fired"); var tEnd = new Date(); if (tEnd.getTime() - tStart.getTime() > 500) { Ti.API.info("Long press"); } });
HTH
-
any luck getting this to work guys?
-
any luck getting this to work guys?