Search code examples
androidandroid-recyclerviewadapterandroid-glide

How to use Glide to load an array of images in an Adapter?


With my current setup, it causes the RecyclerView to lag since I have a lot of resource images. How do I use Glide to load the array of images instead of holder.item_img.setImageResource(INF.getImage_id()); in my ViewHolder?

ArrayList<Information> information = new ArrayList<Information>();
Context ctx;

public InformationAdapter(ArrayList<Information> information, Context ctx)
{
    this.information = information;
    this.ctx = ctx;
}

@Override
public InformationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_layout,parent,false);
    InformationViewHolder informationViewHolder = new InformationViewHolder(view,ctx,information);
    return informationViewHolder;
}

@Override
public void onBindViewHolder(InformationViewHolder holder, int position) {
    Information INF = information.get(position);
    holder.item_img.setImageResource(INF.getImage_id());
    holder.item_name.setText(INF.getName());
    holder.item_location.setText(INF.getLocation());

}

...

Solution

  • Glide.with(ctx)
        .load(information.get(position).getImage_id()‌​)
        .placeholder(R.draw‌​able.loading)
        .into(h‌​older.item_img);