显示具有多个列和行(nxn)的图像在Android中存在内存问题

时间:2013-08-21 04:09:31

标签: android android-listview scrollview universal-image-loader

我在滚动视图中显示属性列表有nxn矩阵视图。这有负载问题(内存耗尽并需要更多时间加载)。为了解决这个问题,我尝试了很少的解决方案,但无法达到100%。

  1. 我将列滚动视图更改为listview并尝试了但结果显示了所有listview中的最后一列值。我知道问题是适配器数据集交换会影响所有查看如何克服这个问题吗?
  2. 最有价值的解决方案是在滚动视图中加载图像,并在屏幕上显示这些图像,并且在可见(滚动)更改时我们可以加载图像。怎么做到这个?
  3. 我尝试使用后台进程(doinbackground)进行图像加载。

2 个答案:

答案 0 :(得分:1)

在gridview或行和列中下载图像的官方android示例 http://developer.android.com/training/displaying-bitmaps/index.html

答案 1 :(得分:0)

试试这段代码:

public class CustomBaseAdapter extends BaseAdapter {
    Context context;
    List<RowItem> rowItems;

public CustomBaseAdapter(Context context, List<RowItem> items) {
    this.context = context;
    this.rowItems = items;
}

/*private view holder class*/
private class ViewHolder {
    ImageView imageView;
    TextView txtTitle;
    TextView txtDesc;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    LayoutInflater mInflater = (LayoutInflater)
        context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
        holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
        holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    RowItem rowItem = (RowItem) getItem(position);

    holder.txtDesc.setText(rowItem.getDesc());
    holder.txtTitle.setText(rowItem.getTitle());
    holder.imageView.setImageResource(rowItem.getImageId());

    return convertView;
}

@Override
public int getCount() {
    return rowItems.size();
}

@Override
public Object getItem(int position) {
    return rowItems.get(position);
}

@Override
public long getItemId(int position) {
    return rowItems.indexOf(getItem(position));
}

}

行项目类是:

public class RowItem {
    private int imageId;
    private String title;
    private String desc;


public RowItem(int imageId, String title, String desc) {
    this.imageId = imageId;
    this.title = title;
    this.desc = desc;
}
public int getImageId() {
    return imageId;
}
public void setImageId(int imageId) {
    this.imageId = imageId;
}
public String getDesc() {
    return desc;
}
public void setDesc(String desc) {
    this.desc = desc;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
@Override
public String toString() {
    return title + "\n" + desc;
}

}