在listView中滚动时的堆大小

时间:2012-07-25 12:42:37

标签: android memory-leaks android-listview heap-memory heap-dump

在列表视图中,当滚动列表视图时,堆大小不断增加,在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;
}

}

上面的代码是使用的List-adapter。我正在使用以下库来显示图像 android-http-image-manager

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

1 个答案:

答案 0 :(得分:1)

ListView非常智能,有许多方法可以回收您的视图以提高性能。其中之一是实施ViewHolder-pattern

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

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

@Override
public int getViewTypeCount() {
    return 2;
}