我搜索了很多,但我无法帮助自己。有没有人可以说如何为我的 ListView 修复 OutOfMemoryError。它仅在将 50 多个位图添加到我的 ListView 后发生。我知道由于所有位图,设备内存已满,但是有没有办法回收远离视口的位图?
这是我的 ListView 的适配器:
public class ImageAdapter extends ArrayAdapter<ExtendedBitmap>{
private LinkedList<ExtendedBitmap> items;
LayoutInflater mInflater = LayoutInflater.from(getContext());
public ImageAdapter(Context context, int resource, LinkedList<ExtendedBitmap> items) {
super(context, resource, items);
this.items = items;
}
public void add(ExtendedBitmap bitmap)
{
items.add(bitmap);
notifyDataSetChanged();
}
public void addFirst(ExtendedBitmap bitmap)
{
items.addFirst(bitmap);
notifyDataSetChanged();
}
public ExtendedBitmap getBitmap(int position){
return items.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.extended_image, parent, false); ;
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.image);
//holder.image.setOnTouchListener(new Touch());
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.image.setImageBitmap(items.get(position).getBitmap());
return convertView;
}
}