Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Sample Project: Dynamically filled scrollViews

The project:
http://www.mediafire.com/?ddms78xjnfq5nf0

Hey Developers!

I again have a sample project for you guys!
These projects are all based on functions I use in my app and had to get it right with trial and error.

So to anyone who ever needs this, they can download and take a look at the code.

Specs:
iOS: 4.1

Titanium SDK: continuous build 1.5.0 (nov 16)

What does it do?:

The app is a simple window with a scrollview in Landscape mode.
What were trying to do is populate the scrollview with imageviews from an array, and give each imageview a eventListener that corresponds to itself.

What can you use this for?:
I used it for 2 purposes.

  1. I have a ebook, with pages and I used this code to generate a scrollview with thumbnails and when clicked on a thumbnail to navigate to that page.
    (i.e: Toy Story)

  2. I also used it on a scrollview with 110 sticker thumbnails and when clicked it put a bigger version on the screen in a webview to drag and pinchzoom.
    (i.e: Drawingpad)

Here is the code for people that don't want to download:

Array:

var stickers = [];
        stickers[0] = {image: './image/Himalayan-Cat.png', stickname: 'cat1', postop: 5};
        stickers[1] = {image: './image/mfnyc_cat17.jpg', stickname: 'cat2', postop: 45};
        stickers[2] = {image: './image/Himalayan-Cat.png', stickname: 'cat3', postop: 5};
        stickers[3] = {image: './image/mfnyc_cat17.jpg', stickname: 'cat4', postop: 45};
        stickers[4] = {image: './image/Himalayan-Cat.png', stickname: 'cat5', postop: 5};
        stickers[5] = {image: './image/mfnyc_cat17.jpg', stickname: 'cat6', postop: 45};
        stickers[6] = {image: './image/Himalayan-Cat.png', stickname: 'cat7', postop: 5};
        stickers[7] = {image: './image/mfnyc_cat17.jpg', stickname: 'cat8', postop: 45};
        stickers[8] = {image: './image/Himalayan-Cat.png', stickname: 'cat9', postop: 5};
        stickers[9] = {image: './image/mfnyc_cat17.jpg', stickname: 'cat10', postop: 45};
        stickers[10] = {image: './image/Himalayan-Cat.png', stickname: 'cat11', postop: 5};
        stickers[11] = {image: './image/mfnyc_cat17.jpg', stickname: 'cat12', postop: 45};
        stickers[12] = {image: './image/Himalayan-Cat.png', stickname: 'cat13', postop: 5};
        stickers[13] = {image: './image/mfnyc_cat17.jpg', stickname: 'cat14', postop: 45};
        stickers[14] = {image: './image/Himalayan-Cat.png', stickname: 'cat15', postop: 5};
        stickers[15] = {image: './image/mfnyc_cat17.jpg', stickname: 'cat16', postop: 45};
        stickers[16] = {image: './image/Himalayan-Cat.png', stickname: 'cat17', postop: 5};
        stickers[17] = {image: './image/mfnyc_cat17.jpg', stickname: 'cat18', postop: 45};    
        stickers[18] = {image: './image/Himalayan-Cat.png', stickname: 'cat19', postop: 5};
        stickers[19] = {image: './image/mfnyc_cat17.jpg', stickname: 'cat5', postop: 45};
        stickers[20] = {image: './image/Himalayan-Cat.png', stickname: 'cat21', postop: 5};

The code:

Titanium.UI.orientation = Titanium.UI.LANDSCAPE_LEFT;

var w1 = Ti.UI.createWindow({
    width: 1024,
    height: 768,
    backgroundColor: '#fff',

    orientationModes : [
    Titanium.UI.LANDSCAPE_LEFT
    ]
});

var scrollViewer = Ti.UI.createScrollView({
    contentWidth: 'auto',
    contentHeight: 160,
    width: 1024,
    height: 160,
    backgroundColor: '#c5c5c5',
    bottom: 50,
    showVerticalScrollIndicator:false,
    showHorizontalScrollIndicator:false,
    zIndex: 10
});

w1.add(scrollViewer);

function add_stickers(e) {
    Ti.API.info("Yay you clicked " + e.source.stick_name);
}

for(var j=0;j<stickers.length;j++) {

    var stickerImg = Titanium.UI.createImageView({
        image: stickers[j].image,
        width: 'auto',
        height: 'auto',
        bottom: stickers[j].postop,
        left: j*180,
        stick_name: stickers[j].stickname
    });
    stickerImg.addEventListener('click', add_stickers);
    scrollViewer.add(stickerImg);
}

w1.open();
— asked November 17th 2010 by Patrick van Zadel
  • addeventlistener
  • code
  • dynamic
  • project
  • sample
  • scrollview
0 Comments

3 Answers

  • are you having any memory issues on the device once fully populated?

    actually i just noticed that a few months back you had a memory issue when scrolling through your app. How did you fix it? thanks for any help as well as for sharing your above code.

    — answered November 17th 2010 by sean oreilly
    permalink
    0 Comments
  • To be honest a lot of memory issues got fixed with the use of continuous builds…while theire not always great they did fix a lot of issues

    If I remember correctly that problem had to do with the way I changed the pages and wile its not completely fixed the issue is gone but my curl up has been removed I constantly created windows the app wasn't all to happy about that, I'm gonna try and get that fixed later this week.

    Also code formatting is essential I noticed instead of adding all views to the main view attach it to a containter view and add this container view to the main view little stuff like that sometimes seem to help, and removing eventlisteners.

    As for this code…no I have 110 stickers and 23 page thumbnails and 10 backgrouds for painting and the only slow down in the app is with animating my drawer menu but no crashes so far the app now weighs about 45 MB but there is still a lot of work to be done and more samples to be uploaded

    — answered November 17th 2010 by Patrick van Zadel
    permalink
    0 Comments
  • hehe 50 views and 9 downloads awesome.

    I'd love some feedback if possible on the code or if it doesn't work.
    just let me know people, lets keep the community alive :D xD

    — answered November 18th 2010 by Patrick van Zadel
    permalink
    1 Comment
    • I have download and tried with your sample code. It is working fine. Cheer man!!! I would like to know do you have any latest code for it? Because I would to know something like this When user click to one of image, need to open new view with user selected image and then there are some unselected images are below. Layout will be like this.

      selected image (see to ###)

      unselected image1, unselected image2, unselected image3,etc..(see to *)

      (### this are need to show only one selected image)

      (* need to show unselected all images from array)

      — commented June 28th 2011 by Pye Phyo Han
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.