Titanium Community Questions & Answer Archive

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

ScrollableView, dynamically adding/removing views

Maybe somebody has ideas on this, I haven't had much luck with responses in the forums here.

I'm trying to build a scrollable view, yet dynamically add/remove views as the user scrolls, similar to a sliding window.

The ScrollableView would potentially have hundreds of views as they scroll, and the idea is to only keep 5 or so views around, and add/remove views on demand. I'm trying to do this with the 'scroll' event.

This sounds nice, but I'm not having luck, especially with removing views. Adding views works, if manually set the "views" member of the ScrollableView.

However, I can't dynamically call addView(), as this messes up the position and the scrollview jumps to the newly added view, even though it shouldn't.

I also can't remove views. Removing a view object from the array (e.g. using JS shift()) will crash the app eventually, and calling removeView() also yields odd behavior - and it plain doesn't work - it's not being removed.

Any ideas would be welcome, the kitchen sink only has a fairly simplified example of this.

At this point I am resigning myself to just keep adding views and crossing my fingers, I'm not sure how to even determine the memory consumption.


— asked November 3rd 2010 by Ingmar Koecher
  • iphone
  • mobile
  • scrollableview
  • I guess another thing I don't understand, is why it's not possible to specify an index/offset when adding a view dynamically - or at least the ability to add a view to the beginning or the end of a scrollable view. It seems as if views are always just added to the end (and even that doesn't seem to work well).

    — commented November 3rd 2010 by Ingmar Koecher
  • It's a bug - this should be possible. You will need to use a workaround until it is fixed, unfortunately.

    — commented November 5th 2010 by Paul Dowsett

2 Answers

  • This is certainly a bug on android. See this Q&A / ticket. I am not sure what the status is for iOS.

    Thus, to insert a view in the middle of a set of scrollable views, the workaround is to remove views in the order that they were added, add the new view, and add back the other views.

    To jump to a specific view, you can use the scrollToView() method.

    — answered November 4th 2010 by Paul Dowsett
  • Thanks, I know about the scrollToView() method.

    I'm not really trying to add something into the middle, but instead at the beginning. As such, if I have a dataset of say 200 objects (say images, views) that I want to scroll over, then I could just have some sort of sliding window where I push/pop views from the start/end as the user scrolls.

    This way I wouldn't have to pre-load the entire dataset. I guess this is turning into a feature request now, but I'm not sure if I'm missing something and this is already possible.

    — answered November 5th 2010 by Ingmar Koecher
    1 Comment
    • Adding a view to the end works for me on android. If you are adding to the beginning, then this is the same issue as adding to the middle - you have to remove all the views that occur after your insertion point (hence, this is just all the views in the stack), add your news views, and then add the original views back in the same order.

      — commented November 5th 2010 by Paul Dowsett
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.