0

在列表视图中滚动列表视图堆大小不断增加,在 Galaxy S3 设备上达到 64MB,应用程序崩溃。

public class GAdapter extends BaseAdapter implements Filterable {

private List<G> list;
private GActivity mContext;
private Drawable attach , text;
private HttpImageManager mHttpImageManager;
private LayoutInflater mInfalotar;

public GAdapter(GActivity context , List<G> list){

    this.list = list;
    mContext = context;
    mInfalotar = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 


    mHttpImageManager = ((GApplication)context.getApplication()).getHttpImageManager();

    attach = mContext.getResources().getDrawable(R.drawable.ic_attachment);
    text   = mContext.getResources().getDrawable(R.drawable.ic_text);
}

public void addContact(VamooseGroup entry){
    list.add(entry);
    notifyDataSetChanged();
}

public void setGroupList(List<G> g){
    list.clear();
    datavalues = null;
    list.addAll(g);
    notifyDataSetChanged();
}

@Override
public int getCount(){
    return (list.size()+1);
}

@Override
public Object getItem(int index) {

    if(index != list.size())
        return list.get(index);
    else
        return null;
}

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

@Override
public int getViewTypeCount() {
    return list.size()+1;
}

@Override
public int getItemViewType(int position) {
    return position == list.size() ? 1 : 0;
}

@Override
public View getView(int index, View convertview, ViewGroup viewGroup) {

        int type = getItemViewType(index);

        switch(type){

            case 0 :

            convertview = mInfalotar.inflate(R.layout.data_listitem , viewGroup, false);

            ImageView myimageView =  (ImageView)convertview.findViewById(R.id.myImage);
            TextView groupname    =  (TextView)convertview.findViewById(R.id.groupname);
            TextView timespam     =  (TextView)convertview.findViewById(R.id.timeInfo);
            TextView msgSender    =  (TextView)convertview.findViewById(R.id.msgowner);
            TextView draft        =  (TextView)convertview.findViewById(R.id.draft);
            ImageView watchIcon   =  (ImageView)convertview.findViewById(R.id.time);
            ImageView attachment  =  (ImageView)convertview.findViewById(R.id.attachment);

            final G g = list.get(index);
            String grouppic = g.pic();
            if(ImageValidator.validate(grouppic)){
                  Bitmap bitmap = mHttpImageManager.loadImage(new HttpImageManager.LoadRequest(Uri.parse(grouppic), myimageView));
                  if (bitmap != null && !bitmap.isRecycled()) {
                        myimageView.setImageBitmap(bitmap);
                  }
            }

            parent.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.select));
            groupname.setText("FOO1");
            msgbox.setText("FOO2");
            groupname.setText("F003");
            msgSender.setText("F004");  
            timespam.setText("F005");
            attachment.setBackgroundDrawable(attach);
            watchIcon.setBackgroundDrawable(text);


        break;

        }

    return convertview;
}

}

上面的代码是使用列表适配器。我正在使用以下库来显示图像 android-http-image-manager

有人能告诉我为什么位图没有被回收吗?滚动列表视图时堆不断增加,达到 64MB。请提供一个简短的解决方案。

4

1 回答 1

1

ListView 很聪明,有很多方法可以回收视图以提高性能。其中之一是实现ViewHolder-pattern

但这不会解决您的问题。您面临的问题是您已经覆盖了该getViewTypeCount()方法并返回了一个巨大的数字。ListView不会回收任何视图,因为对于每一行,您都有不同的类型。

您可以通过不覆盖此方法或返回不同行类型的数量来解决您的问题。这意味着.. 如果您的列表有 2 种不同的布局可用。你返回 2。

@Override
public int getViewTypeCount() {
    return 2;
}
于 2012-07-25T12:55:09.977 回答