如果您只是使用位圖背景,那么@hp.android的答案很好,但是,在我的例子中,我有一個(gè)BaseAdapter
提供一套ImageView
S代表aGridView
..我修改了unbindDrawables()
方法,以便條件是:
if (view instanceof ViewGroup && !(view instanceof AdapterView)) {
...}
但是問(wèn)題是,遞歸方法從不處理AdapterView
..為了解決這一問(wèn)題,我做了以下工作:
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++)
unbindDrawables(viewGroup.getChildAt(i));
if (!(view instanceof AdapterView))
viewGroup.removeAllViews();}
使孩子們AdapterView
仍然被處理-該方法只是不嘗試刪除所有的子級(jí)(這是不支持的)。
然而,這并不能完全解決這個(gè)問(wèn)題,因?yàn)?/trans>ImageView
S管理一個(gè)不是他們背景的位圖。因此,我補(bǔ)充如下。這并不理想,但有效:
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 < viewGroup.getChildCount(); i++)
unbindDrawables(viewGroup.getChildAt(i));
if (!(view instanceof AdapterView))
viewGroup.removeAllViews();
}}
我希望有一個(gè)更有原則的方法來(lái)釋放這些資源。