【问题标题】:Work With Picasso and recycler view to show a list of images使用 Picasso 和回收站视图来显示图像列表
【发布时间】:2018-02-06 09:54:26
【问题描述】:

我有一个项目列表 (6),我正在使用 Picasso 和 OkHttp 库下载并在我的回收站视图中显示图像。

我的回收站视图中有 6 个项目,其中 3 个(1,2,4)在服务器上有图像,而项目 3,5 和 6 没有图像。但在我的回收站视图中,项目 1、2、4 的下载图像也会显示项目 3、5 和 6!我不知道问题出在哪里。 这是我的代码,来自适配器,使用 Picasso 下载和加载图像:

   public class BaseListAdapter extends RecyclerView.Adapter<BaseListAdapter.ViewHolder> {
  private List<PList> menuItems;
  private Context mContext;
  private ActivitySingleGroup activitySingleGroup;


  //Bottom Sheets Views Declaration
  private TextView txtSelectedProduct;
  private TextView txtPRemark;
  private TextView txtQty;
  private EditText edtUserQty;
  private Button btnBuy;
  private Button btnDiscard;
  private ViewPager mPager;
  private CircleIndicator indicator;

  public BaseListAdapter(List<PList> menuItems, Context mContext) {
    this.menuItems = menuItems;
    this.mContext = mContext;
    this.activitySingleGroup = (ActivitySingleGroup) mContext;
  }

  public static class ViewHolder extends RecyclerView.ViewHolder {

    TextView txtName;
    TextView txtPrice;
    ImageView imgDefault;
    LinearLayout parentLayout;
    CoordinatorLayout coordinatorLayout;

    public ViewHolder(View v) {
      super(v);

      txtName = (TextView) v.findViewById(R.id.txtName);
      txtPrice = (TextView) v.findViewById(R.id.txtPrice);
      imgDefault = (ImageView) v.findViewById(R.id.img_defaultImage);
      parentLayout = (LinearLayout) v.findViewById(R.id.parentLayout);
      coordinatorLayout = (CoordinatorLayout) v.findViewById(R.id.coordinatorLayout);

    }
  }

  @Override
  public BaseListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    // Create a new View
    final View v = LayoutInflater.from(activitySingleGroup).inflate(R.layout.activity_normal_group_recycler, parent, false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
  }

  @Override
  public void onBindViewHolder(final ViewHolder holder, final int position) {


    //Download and Load Default Image from server into imgDefault ImageView
    Picasso picasso;
    OkHttpClient client = null;
    String url = "https://bartarinapp-irdeveloper.rhcloud.com/api/images/download/";

    //Handel situations that default image variables will be null
    if (menuItems.get(position).getPDefaultImage() != null &&
      menuItems.get(position).getPDefaultImage().getDefault() != null) {

      if ((menuItems.get(position).getPDefaultImage().getDefault()) &&
        (menuItems.get(position).getPDefaultImage().getIId() != null)) {

        url += menuItems.get(position).getPDefaultImage().getIId();

        client = ServerClass.downloadImage(
          menuItems.get(position).getPDefaultImage().getIId(),
          holder.imgDefault,
          activitySingleGroup);

        picasso = new Picasso.Builder(mContext)
          .downloader(new OkHttp3Downloader(client))
          .listener(new Picasso.Listener() {
            @Override
            public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
              //Here your log
              Log.i("I_ERROR", "Error is: " + exception.toString());
            }
          })
          .build();

      } else {
        url = null;
        picasso = new Picasso.Builder(mContext)
          .listener(new Picasso.Listener() {
            @Override
            public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
              //Here your log
              Log.i("I_ERROR", "Error is: " + exception.toString());
            }
          })
          .build();

      }
    } else {
      url = null;
      picasso = new Picasso.Builder(mContext)
        .listener(new Picasso.Listener() {
          @Override
          public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
            //Here your log
            Log.i("I_ERROR", "Error is: " + exception.toString());
          }
        })
        .build();
    }


    picasso.cancelRequest(holder.imgDefault);
    if (url != null && url.length() > 0) {

      //put here picaso image load code
      picasso.load(url)
        .placeholder(R.drawable.loading_01)
        .error(R.drawable.loading_02)
        .into(holder.imgDefault);

    } else {
      holder.imgDefault.setImageDrawable(null);
    }

    holder.txtName.setText(menuItems.get(position).getPName());
    holder.txtPrice.setText(String.valueOf(menuItems.get(position).getPPrice()));
    holder.parentLayout.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {

        prepareBottomSheet(view, position, holder.coordinatorLayout);
      }
    });
  }

  @Override
  public int getItemCount() {
    if (menuItems.size() > 0) {

      return menuItems.size();

    } else {
      return 0;
    }

  }

感谢您的帮助。

【问题讨论】:

  • 你遇到了什么错误请显示
  • url 是全局变量吗?并在加载图像之前,检查 url 是否为空。
  • 不,我只是从服务器获取带有其 ID 的每个项目。我检查了一下,它们是正确的。

标签: android android-recyclerview picasso okhttp


【解决方案1】:

使用 Picasso 在图像视图中加载图像之前只需添加以下一行

    Picasso.with(this.context).cancelRequest(holder.imageView);
    if(imgUrl!=null && imgUrl.trim().length()>0){
        //put here picaso image load code
    }else {
        holder.imageView.setImageResource(R.drawable.no_img);
    }

【讨论】:

  • 好的,没问题。只需检查 url 是否为空,然后设置要在找不到图像 url 时显示的手动可绘制对象。
  • 我使用了您的代码并用它更新了我的问题,但它也不起作用。我不知道我的代码有什么问题。
  • 嗨,Ehsan 只是尝试将图像视图中的 null 的任何可绘制 else 放在 else 中并尝试,现在我们已经完成了所有可能的操作
【解决方案2】:

这样的库,例如 picaso 和 glide 没有什么问题

但是我正在使用 glide 库,我也遇到了这个问题,但是通过以下代码解决了,请尝试此代码...

依赖

compile 'com.github.bumptech.glide:glide:4.0.0'

    Glide.with(context.getApplicationContext()).load(imageUrl).signature(new StringSignature(String.valueOf(System.currentTimeMillis()))).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);

【讨论】:

    【解决方案3】:

    使用 Picasso.get()

            Picasso.get().load(mImageUrls.get(i))
                .error(R.drawable.loading_erro)
                .placeholder(R.drawable.loading)
                .into(viewHolder.imgItem, new Callback() {
                            @Override
                            public void onSuccess() {
    
                            }
    
                            @Override
                            public void onError(Exception e) {
    
                            }
                        });
    

    【讨论】:

      猜你喜欢
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-22
      • 2017-08-02
      • 1970-01-01
      • 2015-02-26
      相关资源
      最近更新 更多