GridView适配器,getView不起作用

时间:2015-04-24 12:31:11

标签: android gridview universal-image-loader getview

我有自定义适配器,我使用Universal Image Loader库。

MyFragment2

ArrayList<String> items = new ArrayList<>();
items.add("drawable://" + R.drawable.q);
mGridView = (GridView) view.findViewById(R.id.grid_view);
mGridView.setAdapter(new GridViewAdapter2(getActivity(), items));

GridViewAdapter2

private final LayoutInflater mInflater;
private ArrayList<String> mItems;

public GridViewAdapter2(Context context, ArrayList<String> items) {
    this.mInflater = LayoutInflater.from(context);
    this.mItems = items;
}

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

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

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    SquareImageView imageView;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.item_gridview, parent, false);
        imageView = (SquareImageView) convertView.findViewById(R.id.imageView1);
    } else imageView = (SquareImageView) convertView;
    ImageLoader imageLoader = ImageLoader.getInstance();
    imageLoader.init(ImageLoaderConfiguration.createDefault(convertView.getContext()));
    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
            .cacheInMemory()
            .cacheOnDisc()
            .build();
    imageLoader.displayImage(mItems.get(position), imageView, options);
    return convertView;
}
...

我的代码出了什么问题?他们编译但没有显示任何项目。并且不会调用getView();

SquareImageView是简单的方形图像视图;

TY

2 个答案:

答案 0 :(得分:0)

1)实例化ImageLoader并创建选项并不属于getView(),这应该做一次,最好在构造函数内。

2)来自UIL的drawable://

文档
  

注意:只有在您真正需要时才使用drawable://!总是考虑到   本地方式加载drawables - ImageView.setImageResource(...)   使用ImageLoader。

你确定你绝对需要这样做吗?

3)看一下BaseAdapter应该如何实现(它们对GridView和ListView的工作方式几乎相同),例如: this one

答案 1 :(得分:0)

@Xcihnegn ty,它的工作!

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    GridViewHolder viewHolder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.item_gridview, parent, false);
        viewHolder = new GridViewHolder();
        viewHolder.mImageView = (SquareImageView) convertView.findViewById(R.id.imageView1);
        convertView.setTag(viewHolder);
    } else viewHolder = (GridViewHolder) convertView.getTag();
    ImageLoader imageLoader = ImageLoader.getInstance();
    imageLoader.init(ImageLoaderConfiguration.createDefault(convertView.getContext()));
    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
            .cacheInMemory()
            .cacheOnDisc()
            .build();
    imageLoader.displayImage(mItems.get(position), viewHolder.mImageView, options);
    return convertView;
}

private static class GridViewHolder {

    public SquareImageView mImageView;
}