寻找 Glide 的上下文 Android

Finding the context for Glide Android

我正在尝试在 GridView 中显示图片。此网格视图由适配器填充。 我总是遇到这样的问题,即应用程序在为新卡充气时非常缓慢。在互联网上搜索后我找到了 Glide,这可能有助于提高性能。

现在我卡住了,因为我无法想象我应该在 Glide.with(...) 传递什么上下文:

Glide.with(**CONTEXT**).load(sights.get(i).mImageRessourceID).into(sightViewHolder.sightPhoto);

构建我使用的卡片:MainActivity -> Fragment -> Adapter。

这是来自适配器的代码:

public class SightCardAdapter extends RecyclerView.Adapter<SightCardAdapter.SightViewHolder> {


public static class SightViewHolder extends RecyclerView.ViewHolder {
    CardView cv;
    TextView sightName;
    ImageView sightPhoto;

    SightViewHolder(View itemView) {
        super(itemView);
        cv = (CardView) itemView.findViewById(R.id.cv);
        sightName = (TextView) itemView.findViewById(R.id.sight_name);
        sightPhoto = (ImageView) itemView.findViewById(R.id.sight_photo);
    }
}

List<Sight> sights;

SightCardAdapter(List<Sight> sights) {
    this.sights = sights;
}


@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {

    super.onAttachedToRecyclerView(recyclerView);
}

@Override
public SightViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
    SightViewHolder pvh = new SightViewHolder(v);
    return pvh;
}

@Override
public void onBindViewHolder(SightViewHolder sightViewHolder, int i) {
    sightViewHolder.sightName.setText(sights.get(i).mName);
    //sightViewHolder.sightPhoto.setImageResource(sights.get(i).mImageRessourceID);
    Glide.with().load(sights.get(i).mImageRessourceID).into(sightViewHolder.sightPhoto); // missing context
}

@Override
public int getItemCount() {
    return sights.size();
}

}

有人可以给我一些提示吗?我无法想象我应该传递什么。

PS:另外,如果有人知道如何让我的代码更有效率,我将不胜感激:)

在给Adapter传递数据的同时也传递了一个参数Context。如果您从作为 MainActivity 子级的 Fragment 调用您的适配器,只需将 getApplicationContext() 作为上下文传递给您的适配器。

SightCardAdapter(Context context,List<Sight> sights) {
     this.sights = sights;
     this.context=context;
}

while calling from Fragment.
SightCardAdapter(getApplicationContext(),sights);

您可以从任何 View 对象中获取 Context

Glide.with(sightViewHolder.sightPhoto.getContext()).load(sights.get(i).mImageRessourceID).into(sightViewHolder.sightPhoto);

为适配器创建对象时传递上下文。,更改了以下代码

public class SightCardAdapter extends RecyclerView.Adapter<SightCardAdapter.SightViewHolder> {


public static class SightViewHolder extends RecyclerView.ViewHolder {
    CardView cv;
    TextView sightName;
    ImageView sightPhoto;


    SightViewHolder(View itemView) {
        super(itemView);
        cv = (CardView) itemView.findViewById(R.id.cv);
        sightName = (TextView) itemView.findViewById(R.id.sight_name);
        sightPhoto = (ImageView) itemView.findViewById(R.id.sight_photo);

    }
}
Context context;
List<Sight> sights;

SightCardAdapter(List<Sight> sights,Context context) {
    this.sights = sights;
    this.context = context;
}


@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {

    super.onAttachedToRecyclerView(recyclerView);
}

@Override
public SightViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
    SightViewHolder pvh = new SightViewHolder(v);
    return pvh;
}

@Override
public void onBindViewHolder(SightViewHolder sightViewHolder, int i) {
    sightViewHolder.sightName.setText(sights.get(i).mName);
    //sightViewHolder.sightPhoto.setImageResource(sights.get(i).mImageRessourceID);
    Glide.with(context).load(sights.get(i).mImageRessourceID).into(sightViewHolder.sightPhoto); // missing context
}

@Override
public int getItemCount() {
    return sights.size();
}

}
Context context;//declare this globle

SightCardAdapter(Context context, List<Sight> sights) {
    this.context=context;// add context in constructor of adapter class
    this.sights = sights;
}

现在,在 Glide 中使用 context,如下所示:

Glide.with(context).load(sights.get(i).mImageRessourceID).into(sightViewHolder.sightPhoto);

像这样在适配器中传递上下文

Private Context mContext;
SightCardAdapter(List<Sight> sights,Context mContext) {
    this.sights = sights;
    this.mContext = mContext;
}

Glide.with(mContext).load("url").into(imageView);

尝试

在创建适配器对象时从 activity 传递上下文或从 itemView 获取它。

Context context = itemView.getContext();
Pass this context to Glide.

高效使用..

private RequestManager glideManager;  

public MyAdater(Context context, Object activityOrFragment){
     if (object instanceof Fragment) {
         glideManager = Glide.with((Fragment) object);
     } else if (object instanceof Activity) {
         glideManager = Glide.with((Activity) object);
     }
}


@Override
public void onBindViewHolder(ViewHolderGeneric viewHolder, int position) {
   glideManager
         .load(item.getResizedImageUrl())
         .placeholder(R.drawable.emptystates_card_bg_none)
         .fitCenter()
         .into(ivGoods);
}


适配器

MyRecyclerAdapter adapter = new MyRecyclerAdapter(mContext, this);  //this -> Activity or Fragment