如何使用 Picasso 库设置图像视图?

How can I set the imageview with Picasso library?

我有我的 GridViewAdapter,我想用我从 Picasso 加载的内容设置图像视图。图像已加载,但未显示在 GridView 中,只有当我单击图像时才会全屏显示图像,就像我单击它时所做的那样。每个答案都会被预估。谢谢

private static LayoutInflater inflater = null;

public GridViewAdapter(Activity a, String[] fpath, String[] fname) {
    activity = a;
    filepath = fpath;
    filename = fname;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
    return filepath.length;

}

public Object getItem(int position) {
    return position;
}

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


public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.gridview_item, null);
    // Locate the TextView in gridview_item.xml
    TextView text = (TextView) vi.findViewById(R.id.text);
    // Locate the ImageView in gridview_item.xml
     image = (ImageView) vi.findViewById(R.id.grid_image);

    // Set file name to the TextView followed by the position

    Picasso.with(parent.getContext()).load(filepath[position]).placeholder(R.drawable.rtrt).fit().centerCrop().into(image);

    // Decode the filepath with BitmapFactory followed by the position


    // Set the decoded bitmap into ImageView
  //  image.setImageBitmap(bmp);
    return vi;
}

}

在此处使用 activity 上下文

 Picasso.with(activity).load(filepath[position]).placeholder(R.drawable.rtrt).fit().centerCrop().into(image);

在您的 getView() 方法中

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

        if (row == null) {
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.titleTextView = (TextView) row.findViewById(R.id.text);
            holder.imageView = (ImageView) row.findViewById(R.id.grid_image);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        GridItem item = mGridData.get(position);
        holder.titleTextView.setText(Html.fromHtml(item.getTitle()));

        Picasso.with(context).load(filepath[position]).into(holder.imageView);
        return row;
    }

    static class ViewHolder {
        TextView titleTextView;
        ImageView imageView;
    }

注意:

1.Make 如果图像路径是本地路径,请确定您正在传递文件。 2.pass 来自 activity 的上下文,您在其中调用适配器 SampleActivity.this

并尝试在适配器构造函数中将 Activity 对象更改为 Context 对象

有关如何使用来自服务器的图像加载网格视图的更多详细信息,请查看Grid Sample

如果您的图像路径是本地的,那么您必须使用文件

File file = new File(filepath[position]);
 Picasso.with(activity).load(file).placeholder(R.drawable.rtrt).fit().centerCrop().into(image);