每隔10秒使用自定义适配器在GridView中更新图像

时间:2015-03-25 10:55:53

标签: android listview gridview picasso custom-adapter

我的Fragment中有Activity。在这个片段中,我展示了GridView,其中填充了自定义适配器。我正在使用Picasso库从网上下载图像。关键问题是我不知道如何每隔10秒更新GridView中的图像(服务器上的图片每10秒更改一次,买入链接保持静态)。

我已经尝试在片段中使用Handler和gridview.invalidate()并且它对我没有帮助:(

如何正确更新listview的内容(无需滚动开始或使用它)的任何帮助都会很有用。

PictureAdapter.java 公共类PicturesAdapter扩展了BaseAdapter {

private Context mContext;
private List<Camera> cameras;

public PicturesAdapter(Context mContext, List<Camera> cameras) {
    this.mContext = mContext;
    this.cameras = cameras;
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    PictureHolder pictureHolder;

    if (convertView == null){

        LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();

        convertView = inflater.inflate(R.layout.picture_item_layout,parent,false);

        pictureHolder = new PictureHolder();
        pictureHolder.pictureItemImageView = (ImageView)convertView.findViewById(R.id.picture_item_image);
        pictureHolder.pictureItemTextView = (TextView)convertView.findViewById(R.id.picture_item_text);

        convertView.setTag(pictureHolder);
    } else {
        pictureHolder = (PictureHolder)convertView.getTag();
    }

    Picasso.with(mContext)
            .load(cameras.get(position).getCameraSysInfoIMGUrl())
            .networkPolicy(NetworkPolicy.NO_CACHE)
            .memoryPolicy(MemoryPolicy.NO_CACHE)
            .into(pictureHolder.pictureItemImageView);
    //Log.i("drew",cameras.get(position).getCameraSysInfoIMGUrl());
    pictureHolder.pictureItemTextView.setText(cameras.get(position).getCameraName());

    return convertView;
}

static class PictureHolder{
    ImageView pictureItemImageView;
    TextView pictureItemTextView;
}

public String getCameraId(int position) {
    return cameras.get(position).getCameraId();
}

}

1 个答案:

答案 0 :(得分:0)

Picasso现在有声明缓存控件。例如,您可以指定类似

的内容
Picasso.with(context)
  .load(url)
  .networkPolicy(NetworkPolicy.NO_CACHE)
  .memoryPolicy(MemoryPolicy.NO_CACHE)
  .into(imageView);

这迫使毕加索重新从网络上获取图像。