My target is to fetch multiple image urls using an API and load those images in a 2d recyclerview. I'm using retrofit for fetching data and Glide for displaying the images in the recyclerview.
Apparantly, there is no issue with fetching the urls. But no image is being loaded in the recyclerview and it isn't displaying any exception or warning either.
Here's how I've initialized the recyclerview: (Class: HdrPhotoFragment.java)
mRecyclerView = view.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(
new GridLayoutManager(getContext(), SPAN_COUNT)
);
Here's what I've done in onResponse()
for fetching the model and setting the adapter for 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);
}
And this is my onBindViewHolder()
method where I've tried to load the image using glide: (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);
}
It is to be noted that the url obtained in onBindViewHolder()
is accurate.
question from:
https://stackoverflow.com/questions/66059127/can-not-load-images-with-glide-in-recyclerview-data-for-recyclerview-is-fethced 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…