Android - 滚动时在GridView上更改图像

时间:2016-09-09 09:36:23

标签: java android gridview scroll

我正在尝试为我的Android应用中的商店元素创建一个自定义适配器,但每次我向下滚动列表时图像似乎都会改变它们的位置,我已经在这里看了一下但是我找到的建议并尝试无济于事。

每个项目都有一个标题,一个图像(通过AsyncTask加载或从LRUCache中检索),下面项目的价格是我用来生成适配器的代码。

public class ShopAdapter extends BaseAdapter {
    public Context context;
    public ArrayList<ShopItem> shopItemList;
    public ImageCache imageCache;
    String imageToLoad;
    Bitmap shopImage;

    public ShopAdapter(Context context, ArrayList<ShopItem> shopItemList) {
        this.context = context;
        this.shopItemList = shopItemList;
        imageCache = new ImageCache(this.context);
    }
    @Override
    public int getCount() {
        return shopItemList.size();
    }

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.shop_list, null);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.textView3);
            holder.image =(ImageView) convertView.findViewById(R.id.imageView);
            holder.price = (TextView) convertView.findViewById(R.id.textView4);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.title.setText(shopItemList.get(position).getTitle());

        shopImage = imageCache.getBitmapFromMemoryCache(shopItemList.get(position).getImage());
        if(shopImage != null) {
            holder.image.setImageBitmap(shopImage);
        } else {
            new LoadImage(context, holder.image).execute(imageToLoad);
        }

        holder.price.setText(shopItemList.get(position).getPrice());
        return convertView;
    }

    static class ViewHolder {
        TextView title;
        ImageView image;
        TextView price;
    }
}

非常感谢任何帮助!

0 个答案:

没有答案