The default Gallery widget on Android does not recycle views - everytime the view for a new position is called the widget always calls the getView
method of the adapter with convertView
set to null.
As you scroll backwards and forwards this ends up in lots of views being created, which the recycler component that the Gallery stores them in does not seem to recycle them quickly enough leading to an OOM situation.
You can test this easily with a few large-ish images as your gallery items, but just a TextView will cause it in the end. Put a log statement with a counter in the getView
method of your adapter also to see how many new views are created.
Does a third-party widget which behaves like a Gallery but that also implements view recycling exist?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…