我正在创建和画廊,其中图像按日期分组。我正在使用每行都有 Gridview 的 ListView。当一行的 Gridview 包含 2000 左右的大量图像时,应用程序崩溃并给出OutOfMemoryError。如何解决这个问题?
Listview的适配器:
public class MyAdapter extends BaseAdapter {
private ArrayList<String> itemDetailsrrayList;
Context mContext;
private LayoutInflater l_Inflater;
public MyAdapter(Context context,
ArrayList<String> results) {
itemDetailsrrayList = results;
l_Inflater = LayoutInflater.from(context);
mContext = context;
}
public int getCount() {
return itemDetailsrrayList.size();
}
public Object getItem(int position) {
return itemDetailsrrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.settinglayout, null);
holder = new ViewHolder();
holder.myGrid = (MyGridView) convertView
.findViewById(R.id.grid);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.myGrid.setAdapter(new ImageAdapter(itemDetailsrrayList.get(position)));
return convertView;
}
static class ViewHolder {
MyGridView myGrid;
}
}
我的自定义 GridView 类
public class MyGridView extends GridView{
public MyGridView(Context context) {
super(context);
}
boolean expanded = false;
public boolean isExpanded() {
return expanded;
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// HACK! TAKE THAT ANDROID!
if (isExpanded()) {
// Calculate entire height by providing a very large height hint.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
LayoutParams params = (LayoutParams) getLayoutParams();
params.height = getMeasuredHeight();
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}