【发布时间】:2023-03-11 01:18:01
【问题描述】:
我正在尝试制作 Double View Pager,并从我的 PagerAdapter 覆盖 destroyItem 函数,就像在下面的代码中一样:
@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、Jelly Bean,甚至在 Gingerbread 和 Ice Cream Sandwich 上一切正常,但是当我尝试在 API 21 及更高版本上测试我的应用程序时,出现内存不足异常。当我调试我的代码时,我看不到问题所在。谁能帮我 ?谢谢。
【问题讨论】:
标签: android android-viewpager android-5.0-lollipop