如何使用Glide在适配器中加载图像数组?

时间:2017-05-04 13:41:48

标签: android android-recyclerview adapter android-glide

使用我当前的设置,它会导致RecyclerView滞后,因为我有很多资源图像。如何在我的ViewHolder中使用Glide加载图像数组而不是 holder.item_img.setImageResource(INF.getImage_id());

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());

}

...

1 个答案:

答案 0 :(得分:1)

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