qq_花开花谢_0
如果您只是使用位图背景,那么@hp.android的答案很好,但是,在我的例子中,我有一个BaseAdapter提供一套ImageViewS代表aGridView..我修改了unbindDrawables()方法,以便条件是:if (view instanceof ViewGroup && !(view instanceof AdapterView)) {
...}但是问题是,递归方法从不处理AdapterView..为了解决这一问题,我做了以下工作:if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i
unbindDrawables(viewGroup.getChildAt(i));
if (!(view instanceof AdapterView))
viewGroup.removeAllViews();}使孩子们AdapterView仍然被处理-该方法只是不尝试删除所有的子级(这是不支持的)。然而,这并不能完全解决这个问题,因为ImageViewS管理一个不是他们背景的位图。因此,我补充如下。这并不理想,但有效:if (view instanceof ImageView) {
ImageView imageView = (ImageView) view;
imageView.setImageBitmap(null);}总体上unbindDrawables()方法是:private void unbindDrawables(View view) {
if (view.getBackground() != null)
view.getBackground().setCallback(null);
if (view instanceof ImageView) {
ImageView imageView = (ImageView) view;
imageView.setImageBitmap(null);
} else if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i
unbindDrawables(viewGroup.getChildAt(i));
if (!(view instanceof AdapterView))
viewGroup.removeAllViews();
}}我希望有一个更有原则的方法来释放这些资源。