0

リスト ビューのスクロール中にヒープ サイズが継続的に増加すると、Galaxy S3 デバイスで 64 MB に達し、アプリケーションがクラッシュします。

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 に達します。簡単な解決策を教えてください。

4

1 に答える 1

1

ListView はスマートであり、ビューをリサイクルしてパフォーマンスを向上させる方法はたくさんあります。これらの 1 つは、 ViewHolder-patternを実装することです。

しかし、これでは問題は解決しません。あなたが直面している問題は、getViewTypeCount()メソッドをオーバーライドして膨大な数を返すことです。ListView行ごとに異なるタイプがあるため、ビューはリサイクルされません。

このメソッドをオーバーライドしないか、異なる行タイプの数を返すことで、問題を解決できます。これは、リストに 2 つの異なるレイアウトを使用できることを意味します。あなたは2を返します。

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