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,请提供简要解决方案。

+0

你也可以发布logcat吗?我的第一个猜测是,你不是回收convertview,而是一直膨胀xml。这可能会导致外存错误。 – 2012-07-25 12:48:33

+2

可能是bcoz你没有遵循查看持有者的方法,一切都保存在内存中。 – 2012-07-25 12:48:47

+0

@Andro Selva我曾尝试使用Viewholder方法,但我所有的视图都是不同的,如果我在运行时为listview创建listItem,那么我应该如何应用ViewHolder approcah。 – user1551487 2012-07-25 13:29:56

回答

1

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

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

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

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

@Martin Van Mierloo请建议一种方式,如果所有视图都是不同类型的,我应该如何使用viewHolder方法。在上面的例子中,如果使用它像if(condition)attachment.setBackgroundDrawable(attach); else attachment.setBackgroundDrawable(null);在这种情况下,ViewHolder不提供正确的结果。 – user1551487 2012-07-25 13:31:10

+1

我发现的最好的教程是[logc.at](http://logc.at/2011/10/10/handling-listviews-with-multiple-row-types/)。这是一个干净利落的ListView实现,具有不同的行。 – DroidBender 2012-07-25 13:33:18

+0

@Martijin Van Mierloo我正在阅读,有没有办法处理位图,他们也似乎没有回收,并不断采取记忆上述库“android-http-image-manager”不使用WeakRefrence方法。 – user1551487 2012-07-25 13:36:00