如果您只是使用位图背景,那么@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();
}}
我希望有一个更有原则的方法来释放这些资源。
博客围绕Android视图资源释放问题展开。针对使用BaseAdapter提供ImageView的情况,修改unbindDrawables()方法处理ViewGroup,但递归方法不处理AdapterView。通过进一步修改使AdapterView子级被处理,还补充处理ImageView的位图。不过仍希望有更优方法释放资源。
721

被折叠的 条评论
为什么被折叠?



