Home > Blockchain >  How Use Glide To load Set of Url images from array
How Use Glide To load Set of Url images from array

Time:02-19

I have a set of links I put them in an array of type String, but I have a problem with glide and postion, what is the best solution for it Conclusion: I want to collect 10 gif images from a website and put them here to be used in a RecyclerView. I just want to solve this code. The error starts from load((android_image_urls[position]))

 public class Imagegroub extends RecyclerView.Adapter<Imagegroub.CustomViewHolder> {
    
        private final List<String> mFileList;
        private final Activity mActivity;
        final String android_image_urls[] = {
                "https://media.giphy.com/media/XqKu2VpXi9Nkj19dqq/giphy.gif",
                "https://media.giphy.com/media/XqKu2VpXi9Nkj19dqq/giphy.gif",
                "https://media.giphy.com/media/XqKu2VpXi9Nkj19dqq/giphy.gif",
                "https://media.giphy.com/media/XqKu2VpXi9Nkj19dqq/giphy.gif",
                "https://media.giphy.com/media/XqKu2VpXi9Nkj19dqq/giphy.gif",
                "https://media.giphy.com/media/XqKu2VpXi9Nkj19dqq/giphy.gif",
                "https://media.giphy.com/media/XqKu2VpXi9Nkj19dqq/giphy.gif",
                "https://media.giphy.com/media/XqKu2VpXi9Nkj19dqq/giphy.gif",
                "https://media.giphy.com/media/XqKu2VpXi9Nkj19dqq/giphy.gif",
                "https://media.giphy.com/media/XqKu2VpXi9Nkj19dqq/giphy.gif"
        };
    
        public Imagegroub(Activity activity, List<String> fileList) {
            mActivity = activity;
            mFileList = fileList;
        }
    
        @Override
        public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.gallery_list_row, parent, false);
            return new CustomViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(CustomViewHolder holder, int position) {
            Glide
                    .with(mActivity)                                 
                    .load((android_image_urls[position]))          // here error
                    .override(200, 200)
                    .centerCrop()
                    .into(holder.imageResource);
    
              final int itemPosition = holder.getBindingAdapterPosition();
              holder.imageResource.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Toast.makeText(mActivity, android_image_urls[itemPosition], Toast.LENGTH_SHORT).show();
                }
            });
        }
    
        @Override
        public int getItemCount() {
            return mFileList.size();
        }
    
        static class CustomViewHolder extends RecyclerView.ViewHolder {
            final ImageView imageResource;
            CustomViewHolder(View itemView) {
                super(itemView);
                this.imageResource = (ImageView) itemView.findViewById(R.id.image_resource);
    
    
    
            }
        }
    }

MainActivity

and here what set in recyclerView.setAdapter()

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_sti);
        requirePermission();
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        recyclerView.setHasFixedSize(true);
        
        recyclerView.setAdapter(new Imagegroub()); // what add here 
 
 
    }

CodePudding user response:

Replece your adapter code :-

public class Imagegroub extends RecyclerView.Adapter<Imagegroub.CustomViewHolder> {

private final Activity mActivity;
final String android_image_urls[] = {
        "https://media.giphy.com/media/XqKu2VpXi9Nkj19dqq/giphy.gif",
        "https://media.giphy.com/media/XqKu2VpXi9Nkj19dqq/giphy.gif",
        "https://media.giphy.com/media/XqKu2VpXi9Nkj19dqq/giphy.gif",
        "https://media.giphy.com/media/XqKu2VpXi9Nkj19dqq/giphy.gif",
        "https://media.giphy.com/media/XqKu2VpXi9Nkj19dqq/giphy.gif",
        "https://media.giphy.com/media/XqKu2VpXi9Nkj19dqq/giphy.gif",
        "https://media.giphy.com/media/XqKu2VpXi9Nkj19dqq/giphy.gif",
        "https://media.giphy.com/media/XqKu2VpXi9Nkj19dqq/giphy.gif",
        "https://media.giphy.com/media/XqKu2VpXi9Nkj19dqq/giphy.gif",
        "https://media.giphy.com/media/XqKu2VpXi9Nkj19dqq/giphy.gif"
};

public Imagegroub(Activity activity) {
    mActivity = activity;
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.gallery_list_row, parent, false);
    return new CustomViewHolder(view);
}

@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
    Glide
            .with(mActivity)
            .asGif()
            .load((android_image_urls[position]))
            .override(200, 200)
            .centerCrop()
            .into(holder.imageResource);

    final int itemPosition = holder.getBindingAdapterPosition();
    holder.imageResource.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        }
    });
}

@Override
public int getItemCount() {
    return android_image_urls.length;
}

static class CustomViewHolder extends RecyclerView.ViewHolder {
    final ImageView imageResource;

    CustomViewHolder(View itemView) {
        super(itemView);
        this.imageResource = (ImageView) itemView.findViewById(R.id.image_resource);

    }
}}

Replece your activity code :-

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(new Imagegroub(this));
  • Related