Replace (update) a View in a ScrollableView in Titanium Mobile

This is the first of a (possible) series of posts concerning appcelerator’s Titanium Mobile.

Sometimes you’ll need to replace one view of a ScrollableView, which, as far as I can tell by the rather incomplete documentation, is not supported out-of-the-box. Don’t worry, it is possible though and I wrote a function for it:

/**
 * Replace (update) one view of a ScrollablView with a new one
 *
 * @param   object  The scrollableView
 * @param   object  The old view to be replaced
 * @param   object  The new view to replace the old one
 * @return  void
 */
function replaceView(scrollableView, oldView, newView)
{
  // loop all the views in the scrollable view
  for (var i = 0, newViews = [], l = scrollableView.views.length; i < l; i++)
  {
    // replace the old view with the new one
    if (i == scrollableView.currentPage)
      newViews.push(newView);
    // leave the other views unchanged
    else
      newViews.push(scrollableView.views[i]);
  }

  // update the scrollableView's views array with the new one
  scrollableView.views = newViews;
}

It’s pretty straightforward, we loop the existing Views of the ScrollableView and copy each View, replacing the one to be change, to a new array. Then we tell the ScrollableView to use our new Views-array for it’s Views.
I hope this helps someone.







2 Responses to “Replace (update) a View in a ScrollableView in Titanium Mobile”

Great stuff, I was just about to wonder how one could update a specific view, now I don’t need to :)

I might need it – I have a problem that you may have run into as well:

I have 24 views, each view has an animation of a door opening (It’s a christmas calendar). Now if I open a door and either use .scrollToView or the arraws to move forth and back, everything is ok. But if I swipe forth and back, the doors that I opened are closed again. It seems like the scrollable has cached the state of the view, because everything works (in other words: It’s not a cached Blob – but the actual view in its initial state)

Have you come across this? Did you find a solution?

I found your page while

Acebone added these pithy words on Nov 26 11 at 11:47 pm

Hi Acebone

I have not come across this issue.
If you don’t find a solution to the problem, you might be able to solve this with a workaround. Maybe add a callback function to the animation event that toggles the image on the scrollableView after the animation completes or something?

Hope you find a way to work it out.
Cheers
Chris

Chris added these pithy words on Nov 29 11 at 11:07 am

Leave a Reply

Spam protection by WP Captcha-Free