Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

android - Can not load images with glide in recyclerview (data for recyclerview is fethced using retrofit)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

this should work (code snippet in Kotlin)

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

check the url also once


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...