【问题标题】:Can not load images with glide in recyclerview (data for recyclerview is fethced using retrofit)无法在 recyclerview 中使用 glide 加载图像(recyclerview 的数据是使用改造来获取的)
【发布时间】:2021-02-05 07:18:49
【问题描述】:

我的目标是使用 API 获取多个图像 url,并将这些图像加载到 2d recyclerview 中。我正在使用改造来获取数据,并使用Glide 在回收站视图中显示图像。

显然,获取网址没有问题。但是 recyclerview 中没有加载任何图像,也没有显示任何异常或警告。

这是我如何初始化 recyclerview:(Class: HdrPhotoFragment.java)

        mRecyclerView = view.findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(
                new GridLayoutManager(getContext(), SPAN_COUNT)
        );

这是我在onResponse() 中为获取模型并为 recyclerview 设置适配器所做的:(Class: HdrPhotoFragment.java)

            @Override
            public void onResponse(Call<List<HdrPhotoListItemModel>> call, Response<List<HdrPhotoListItemModel>> response) {
                Log.d(TAG, "onResponse: " + response.code());
                if (response.code() != 200) {
                    Log.d(TAG, "onResponse: error fetching image data. CODE: ");
                    Toast.makeText(getContext(), "Error loading images", Toast.LENGTH_SHORT).show();
                    return;
                }
                mHdrPhotoModelList = response.body();
//                Log.d(TAG, "onResponse: response body" + response.body().toString());
                for (HdrPhotoListItemModel model : mHdrPhotoModelList) {
                    Log.d(TAG, "onResponse: Model = " + model.toString());
                }
                HdrPhotoListItemAdapter adapter = new HdrPhotoListItemAdapter(mHdrPhotoModelList, HdrPhotoFragment.this);
                mRecyclerView.setAdapter(adapter);
            }

这是我尝试使用 glide 加载图像的onBindViewHolder() 方法:(Class: HdrPhotoListItemAdapter.java)

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        HdrPhotoListItemModel itemModel = mHdrPhotoList.get(position);
//        holder.imageView.setImageResource(itemModel.getImageUrl());
        String url = Constants.BASE_URL + itemModel.getImageUrl();
        Log.d(TAG, "onBindViewHolder: URL: " + url);
        Glide.with(holder.imageView.getContext())
                .load(url)
                .into(holder.imageView);
    }

需要注意的是onBindViewHolder()中得到的url是准确的。

【问题讨论】:

    标签: android android-recyclerview retrofit2 android-glide


    【解决方案1】:

    这应该可以 (Kotlin 中的代码 sn-p)

    Glide.with(holder.itemView.context).load(url).into(holder.itemView.findViewById(R.id.image))
    

    也检查一次网址

    【讨论】:

      【解决方案2】:

      我不确定图片未加载的确切原因。使用 Picasso 库而不是 Glide 解决了这个问题。

      onBindViewHolder()方法里面,我只是换了

              Glide.with(holder.imageView.getContext())
                      .load(url)
                      .into(holder.imageView);
      

      下面这行:

              Picasso.get().load(url).into(holder.imageView);
      

      虽然它有效,但如果有人能告诉我为什么它不能与 Glide 一起使用,我将不胜感激。我的猜测是它与上下文有关。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多