Reputation: 13154
I'm implementing a custom layout. It's a heterogenous grid and will look like the one below, except for the fact, that the tiles will be images with some text. Additionally it has an animation effect - each time the underlying data is changed (items added, removed, reordered) there's an animation, each tile moves to its new position. And this works perfectly.
The whole thing is inside a ScrollView
. When user starts app, a few tiles (20 at most) appear. As he reaches the bottom of the scroll, new tiles get loaded. The tiles are RelativeLayouts
.
My question concerns the recycling of views. Typical layouts which accept multiple subviews like ListView
or GridView
have a recycling mechanism. On scroll, when a view is not visible, it's recycled and it comes back when it's about to be seen by the user again.
Do I need to implement such a behaviour? What would happen if I don't? Is it even possible to implement it since I may have to animate the sub-views (also these which wouldn't be in the visible part of the layout) to their new positions when new data arrives.
Or maybe I should just recycle the bitmaps when they're outside the visible area? This is probably where I could hit an OutOfMemory, isn't it?
Upvotes: 3
Views: 2270
Reputation: 6754
I'd definitely recommend recycling, especially if the number of items is effectively unlimited.
Aside from memory issues which, like you say, will largely depend on handling of Bitmaps, not recycling will cause more chance of juddering when you load in more items. This is because more Views will need to be created - rather than recycled.
Implementing View recycling should be quite straightforward given you're using a custom layout, just keep a cache of Views and follow the Pattern similar to Android's Adapter class (i.e. getView(int position, View convertView ..)
. When you need to animate a View from offscreen, you'll use getView()
to get an unused View and update it to contain your model data (if you're not using MVC for this, I'd recommend it).
Upvotes: 2