1

我正在开发安卓应用程序。我的应用程序通过 image_url 使用 ImageLoader 库(例如 Picasso、Glide 或 AUIL)显示来自服务器的许多图片。

主要活动中有五个片段。每个片段都有许多图像列表视图。

我的问题是,单击特定图像视图后,会创建新活动,然后创建具有许多图像列表视图的片段。我单击了不同的图像,并创建了新的活动。发生此手势时,未获取堆内存...

在三个库中,AUIL 是其中最好的。

我尝试了基本适配器的递归方法和可绘制的 null 回调等......

但这种情况并没有得到解决。

这是我的示例适配器源:

public class MagazineRelBrandItemAdapter extends BaseAdapter {

ArrayList<String> receivedList;
ArrayList<View> brandThumList = new ArrayList<>();
PrintLog printLog = new PrintLog(Application.isDEBUG(), "MagazineRelBrandItemAdap ", "created");
Activity mActivity;

public MagazineRelBrandItemAdapter(ArrayList<String> dataList, Activity activity) {
    receivedList = dataList;
    mActivity = activity;
}

@Override
public int getCount() {
    return receivedList.size();
}

@Override
public String getItem(int position) {
    return receivedList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MagazineRelBrandView itemView;
    if (convertView == null) {
        itemView = new MagazineRelBrandView(Application.getContext(),mActivity);

    } else {
        itemView = (MagazineRelBrandView) convertView;

    }
    try {
        itemView.setContent(receivedList.get(position));
        brandThumList.add(itemView);
    } catch (OutOfMemoryError e) {
        if (mRecycleList.size() <= parent.getChildCount()) {
            printLog.cutsomPrintLog(Application.isDEBUG(), "img recycle size comment>>", mRecycleList.size() + "");
            throw e;
        }
        recycleHalf();
        System.gc();
        return getView(position, itemView, parent);
    }
    mRecycleList.add(new WeakReference<View>(itemView));
    return itemView;
}

private List<WeakReference<View>> mRecycleList = new ArrayList<WeakReference<View>>();

public void recycleHalf() {
    int halfSize = mRecycleList.size() / 2;
    List<WeakReference<View>> recycleHalfList = mRecycleList.subList(0, halfSize);
    RecycleUtils.recursiveRecycle(recycleHalfList);
    for (int i = 0; i < halfSize; i++)
        mRecycleList.remove(0);
}

public void recycle() {
    RecycleUtils.recursiveRecycle(mRecycleList);
}
}

这是我的小部件:

public class MagazineRelBrandView extends LinearLayout {
Activity mActivity;

public MagazineRelBrandView(Context context,Activity activity) {
    super(context);
    mActivity = activity;
    init();
}

CircleImageView brandLogo;
TextView brandName;
ImageView brandBg;
View clickV;

private void init() {

    inflate(getContext(), R.layout.magazine_rel_brand_view, this);
    brandBg = (ImageView) findViewById(R.id.magazine_rel_brand_bg);
    brandName = (TextView) findViewById(R.id.magazine_rel_brand_name);
    brandLogo = (CircleImageView) findViewById(R.id.magazine_rel_brand_icon);
    clickV = findViewById(R.id.content_click_view);
    brandName.setTypeface(Typeface.createFromAsset(getContext().getAssets(), CommonKeys.FuturaFont));
    clickV.setOnClickListener(goBrandDetailPageListener);
}


public void setContent(String brandName) {
    this.brandName.setText(brandName);
    try {
        String brandTitleUrl;
        brandTitleUrl = URLEncoder.encode(brandName, "UTF-8");
        totalThumbUrl = CommonKeys.brandThumbUrl + brandTitleUrl;
        totalBackUrl = CommonKeys.brandBackgroundUrl + brandTitleUrl;

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    showBG(brandName);
    showLogo(brandName);
}
String totalThumbUrl;
String totalBackUrl;

public void showLogo(final String brandName){
    Glide.with(mActivity).load(totalThumbUrl).override(DpToPxUtil.dp2px(38),DpToPxUtil.dp2px(38)).into(brandLogo);

}public void showBG(final String brandName){
    Glide.with(mActivity).load(totalBackUrl).override(DpToPxUtil.dp2px(120),DpToPxUtil.dp2px(144)).into(brandBg);
}

public String getBrandName() {
    return brandName.getText().toString();
}

Handler brandHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case 0:
                MyToast.show("" + msg.obj);
                break;
            case 1:

                BrandInfoZip brandInfoZip = (BrandInfoZip) ((BrandInfoParentZip) msg.obj).getData();

                BrandListZip getBrandZip = new BrandListZip();
                getBrandZip.setIs_following(brandInfoZip.is_following());
                getBrandZip.setBrand_name(getBrandName());

                Intent goBrandPage = new Intent(Application.getContext(), BrandDetailActivity.class);
                goBrandPage.putExtra("brandZip", getBrandZip);
                goBrandPage.putExtra("title", getBrandName());
                goBrandPage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                Application.getContext().startActivity(goBrandPage);
                break;
        }
    }
};

OnClickListener goBrandDetailPageListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        BrandDescriptionZip brandDescriptionZip = new BrandDescriptionZip();
        brandDescriptionZip.setBrand_name(getBrandName());
        new GetBrandInfoFromServer(brandHandler).execute(brandDescriptionZip);

    }
};
}

和片段 onDestroyView 源:

 @Override
public void onStop() {
    super.onStop();
    if (upperBgImgV.getDrawable() != null) {
        upperBgImgV.getDrawable().setCallback(null);
    }
    if (writerThumb.getDrawable() != null) {
        writerThumb.getDrawable().setCallback(null);
    }
    if (otherContentV.getDrawable() != null) {
        otherContentV.getDrawable().setCallback(null);
    }
}

@Override
public void onDestroyView() {
    Glide.clear(writerThumb);
    magazineRelBrandItemAdapter.recycle();
    unbindDrawables(getView());
    //unbindDrawables(writerThumb);
    //unbindDrawables(otherContentV);
    System.gc();
    super.onDestroyView();
    Log.i(TAG, "onDestroyView-");
}
private void unbindDrawables(View view) {
    if (view == null)
        return;

    if (view instanceof ImageView) {
        ((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));
        }
        view.setBackgroundResource(0);
        view.setBackgroundDrawable(null);
    }
}

这是 onDestroy() 课程的活动:

@Override
protected void onDestroy() {
    RecycleUtils.recursiveRecycle(getWindow().getDecorView());
    System.gc();
    Glide.get(this).clearMemory();
    FlurryAgent.onEndSession(Application.getContext());
    printLog.cutsomPrintLog(Application.isDEBUG(),"Magazine Detail Activity onDestroy-","onDestroy-");
    super.onDestroy();
    finish();
}

这些当然是用来清除内存堆的。但内存泄漏仍在继续......

请帮我。

4

0 回答 0