在PagerAdapter Android的棒棒糖unbindDrawables不工作 [英] unbindDrawables at PagerAdapter Android Lollipop don't work

查看:442
本文介绍了在PagerAdapter Android的棒棒糖unbindDrawables不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使双查看传呼机,我从我的PagerAdapter覆盖 destroyItem 功能就像在code波纹管:

I am trying to make Double View Pager and I override destroyItem function from my PagerAdapter just like in code bellow:

 @Override
public void destroyItem(ViewGroup container, int position, Object object)
{
    container.removeView((View) object);
    unbindDrawables((View) object);
    System.gc();
    object = null;
}

protected void unbindDrawables(View view)
{
    if (view instanceof ImageView)
    {
        Drawable drawable = ((ImageView) view).getDrawable();
        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            Bitmap bitmap = bitmapDrawable.getBitmap();
            bitmap.recycle();
        }
        ImageWorker.cancelWork(((ImageView) view));
        ((ImageView) view).setImageResource(0);
        ((ImageView) view).setImageDrawable(null);
    }
    if (view.getBackground() != null)
    {
        view.getBackground().setCallback(null);
    }
    if (view instanceof ViewGroup)
    {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++)
        {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
        }
        if (!(view instanceof AdapterView<?>))
        {
            ((ViewGroup) view).removeAllViews();
        }
        //((ViewGroup) view).removeAllViews();
    }
}

一切是Android KitKat上,果冻豆OK,甚至在姜饼和Ice Cream Sandwich的,但是当我尝试上试21 API和更高的我的应用程序,我有内存不足的异常。当调试我的code,我看不到的问题。谁能帮我?谢谢你。

Everything is OK on Android KitKat, Jelly Bean, even on Gingerbread and Ice Cream Sandwich, but when I try to test my app on API 21 and higher, I have Out of memory exception. When I debug my code, I can't see the problem. Can anyone help me ? Thanks.

推荐答案

我无法从该ImageView的是从视图层次结构中移除了code看到的,如果unbindDrawables被呼吁ImageView的,而不是一个ViewGroup那么它不会工作,除去ImageView的位图泄漏。我曾与出内存有类似的问题,原来是其中美元的位图的byte []的对$ pvented GC的ImageView的,而且我发现唯一的解决办法是删除从视图层次结构的ImageView的(和扔掉参考) 。这是不够的,我做以下清理

I cannot see from the code that the imageview is removed from the view hierarchy, if the unbindDrawables is called on the imageView and not the viewgroup then it wont work to remove the imageview bitmap leak. I had a similar problem with out of memory that turned out to be the ImageView which prevented GC of byte[] from bitmap, and the only solution I found was to remove the ImageView from view hierarchy (and throw away the reference). It was NOT enough for me to do the following cleanup

    if (imageView.getBackground() != null) {
        imageView.getBackground().setCallback(null);
    }
    setImageBackground(imageView, null);
    imageView.setImageBitmap(null);
    imageView.setImageDrawable(null);

我也有很多采用Android Studio的内存分析此问题,围绕重点问题。

I also had lots of issues with Android Studio memory analyzer which this question is focused around.

<一个href=\"http://stackoverflow.com/questions/34999781/leaked-unreferenced-byte-originally-from-bitmap-but-recycled-causing-memory\">Leaked未引用的byte [最初是从位图],但回收()导致内存泄漏(直到活动停止)

这篇关于在PagerAdapter Android的棒棒糖unbindDrawables不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆