Skip to content

Instantly share code, notes, and snippets.

@johncblandii
Last active December 14, 2015 10:49
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 johncblandii/5074660 to your computer and use it in GitHub Desktop.
Save johncblandii/5074660 to your computer and use it in GitHub Desktop.
Add a view at a specific position in a list. Original inspiration: https://gist.github.com/iskugor/1485751.
Alloy.Globals.insertViewAt = function(parent, element, index) {
var children = parent.children.slice(0);
for(var i = 0; i < children.length; ++i){
parent.remove(children[i]);
}
if(index >= 0) {
var tmp = children.slice(0, index);
tmp.push(element);
children = tmp.concat(children.slice(index));
}
for (i = 0; i < children.length; ++i) {
parent.add(children[i]);
}
}
/**
* Usage:
* var view = Ti.UI.createView({backgroundColor: "red"});
* Alloy.Globals.insertViewAt($.list, view, 1); //where '$.list' is a view in your alloy XML
*/
@johncblandii
Copy link
Author

Note: This is not setup to be failure proof (passing a bad index may be problematic) but when used properly it will work just fine.

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