给出OutOfMemoryError的ListView里面的GridView

时间:2013-05-04 19:58:20

标签: android android-listview android-gridview

我正在创建和图库按日期分组图像。我正在使用ListView,每行都有Gridview。当一行的Gridview包含大量关于2000的图像时,app崩溃了,给出了 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);
    }
}

}

0 个答案:

没有答案