Titanium Community Questions & Answer Archive

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

Dynamic Scrollable View with 3 views at a time

I'm not understanding this part: "e.source.name2 is a unique int for each page". I'm not sure what I'm missing here. There's something with the "pages" that I'm not getting right. I've tried loaded multiple views on the "pages" array and giving them a name, but that didn't work either. Each image is named as follows: 001.jpg, 002.jpg, 003.jpg, etc. The images reside in a folder called "images"

Can someone please explain it to me?

var pages = [
1,2,3,4,5
];

var scrollView = Titanium.UI.createScrollableView({
    views:pages,
    showPagingControl:false,
    pagingControlHeight:30,
    enableZoomControls:false,
    currentPage:1
});

win.add(scrollView);

scrollView.addEventListener('scroll', function(e){

    var pagenumber = e.source.pages;
    Titanium.API.info(pagenumber);

//pass in the unique identifier for each view

var onepageback = Math.round(pagenumber) - 1;  
//grab identifiers for the preceding and following view so that its seamless when the user scrolls

var onepagefwd = Math.round(pagenumber) + 1;

    for (var q=0; q<pages.length; q++){ 
//erase all backgrounds except for the current one by assigning them an invalid image

if (q == pagenumber){

            //do nothing to currentpage 

            }

else{ //erase all others

pages[q].setbackgroundImage('');
            }
    }

    //create views for 2 adjacent pages
        pages[onepageback].setbackgroundImage('images/00' + onepageback + '.jpg');

            pages[onepagefwd].setbackgroundImage('images/00' + onepagefwd + '.jpg');
});
— asked December 6th 2010 by Arlton Lowry
  • array
  • images
  • int
  • scrollableview
  • scrollview
  • source
0 Comments

4 Answers

  • Accepted Answer

    I would suggest you do something like this.. Change your loadViews function to this… and put the addImageToView function below it.

    // LOAD VIEWS
    function loadViews(index)
    {
        loadedViews = [];
        var selectedPage = 0;
        var i = 0;
    
        if(index > 0)
        {
            i = index - 1;
            var v1 = Titanium.UI.createView({index:i});
            addImageToView(v1, viewCollection[i]);
            loadedViews.push(v1);
            selectedPage = 1;
        }
    
            i = index;
            var v2 = Titanium.UI.createView({index:i});
            addImageToView(v2, viewCollection[i]);
            loadedViews.push(v2);
    
        if(index < viewCollection.length - 1)
        {
            i = index + 1;
            var v3 = Titanium.UI.createView({index:i});
            addImageToView(v3, viewCollection[i]);
            loadedViews.push(v3);
        }
        else
        {
            selectedPage = 1;
        }
    
        scroller.views = loadedViews;
        scroller.currentPage = selectedPage;
        lastView = loadedViews[scroller.currentPage];
    }
    
    function addImageToView(view, imageUrl)
    {
       var imageView = Titanium.UI.createImageView({image:imageUrl});
       view.add(imageView);
    }
    
    — answered December 6th 2010 by Michael Huntington
    permalink
    0 Comments
  • Wow, I needed this same feature today.. I got it working about an hour ago. Here's my code.

    http://pastie.org/1352978

    I hope that helps.

    — answered December 6th 2010 by Michael Huntington
    permalink
    2 Comments
    • Haha! Much thanks.

      — commented December 6th 2010 by Arlton Lowry
    • I can't seem to get the Titanium.UI.createImageView to load the images. Here's what I've got:

      var viewCollection = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
          viewCollection[0] = "001.jpg";
          viewCollection[1] = "002.jpg";
          viewCollection[2] = "003.jpg";
          viewCollection[3] = "004.jpg";
          viewCollection[4] = "005.jpg";
          viewCollection[5] = "006.jpg";
          viewCollection[6] = "007.jpg";
          viewCollection[7] = "008.jpg";
          viewCollection[8] = "009.jpg";
          viewCollection[9] = "010.jpg";
      
      Ti.API.info('array '+ viewCollection);
      
      var loadedViews = [];
      
      
      // SCROLLER
      var scroller = Titanium.UI.createScrollableView({
          showPagingControl:false,
          pagingControlHeight:30,
          enableZoomControls:false
      });
      scroller.addEventListener('scroll', function(e){
      
          if (e.currentView != lastView.index) {
      
              loadViews(e.view.index)
          }
      
          Titanium.API.info(loadViews);
      
      });
      
      win.add(scroller);
      
      // LOAD VIEWS
      function loadViews(index)
      {
          loadedViews = [];
          var selectedPage = 0;
      
          if(index > 0)
          {
              var v1 = Titanium.UI.createImageView({index:index - 1});
              loadedViews.push(v1);
              selectedPage = 1;
          }
      
              var v2 = Titanium.UI.createImageView({index:index});
              loadedViews.push(v2);
      
          if(index < viewCollection.length - 1)
          {
              var v3 = Titanium.UI.createImageView({index:index + 1, image: viewCollection});
              loadedViews.push(v3);
          }
          else
          {
              selectedPage = 1;
          }
      
          scroller.views = loadedViews;
          scroller.currentPage = selectedPage;
          lastView = loadedViews[scroller.currentPage];
      }
      
      // Load the initial view you want by passing the index of the view from your viewCollection
      loadViews(3);
      

      Do you know what I'm missing?

      — commented December 6th 2010 by Arlton Lowry
  • Ooops.. I forgot to mention.. in my code I'm using the shorthand redux code to create my views.. so "new View()" is really Titanium.UI.createView();

    Just change all those… but you get the idea.

    — answered December 6th 2010 by Michael Huntington
    permalink
    2 Comments
    • I really do appreciate your help. This has been driving me crazy.

      — commented December 6th 2010 by Arlton Lowry
    • On this line of code, did you mean "viewCollection", instead of "collection"?

          if(index < collection.length - 1)
          {
              var v3 = Titanium.UI.createView({index:index + 1,backgroundColor: '#cc0000'});
              loadedViews.push(v3);
          }
          else
          {
              selectedPage = 1;
          }
      

      — commented December 6th 2010 by Arlton Lowry
  • Hi ,

    Please the bellow it may be help you,

    Link :- Dynamic View

    — answered July 7th 2012 by Nishanth S
    permalink
    0 Comments
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.