查找Glide的上下文Android
我试图在GridView中显示图片。此网格视图由适配器填充。 我总是遇到这样的问题,即应用程序膨胀新卡时非常滞后。在互联网搜索后,我发现了Glide,这可能有助于提高性能。查找Glide的上下文Android
现在我坚持,因为我无法想象我应该Glide.with通过在什么情况下(...):
Glide.with(**CONTEXT**).load(sights.get(i).mImageRessourceID).into(sightViewHolder.sightPhoto);
要构建我用卡:MainActivity - >片段 - >适配器。
这是从适配器代码:
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:此外,如果任何人有一定的窍门如何使我的代码更有效,我将不胜感激:)
在将数据传递到适配器也传递参数上下文。如果您从MainActivity的子项Fragment调用Adapter,只需将getApplicationContext()作为上下文传递给Adapter。
SightCardAdapter(Context context,List<Sight> sights) {
this.sights = sights;
this.context=context;
}
while calling from Fragment.
SightCardAdapter(getApplicationContext(),sights);
我已经试过这个解决方案。看起来像: 滑翔。与(getApplicationContext())的负载(sights.get(ⅰ).mImageRessourceID).into(sightViewHolder.sightPhoto)。 但Android Studio认为无法解析方法:S –
Glide.with(getApplicationContext())您无法在适配器中使用getApplicationContext()。从片段发送此上下文到您的适配器。 –
您可以从任何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);
尝试
无论是在创建适配器对象从活动传递上下文或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
通行证上下文适配器 - 以同样的方式,你SightViewHolder(查看ItemView控件,上下文语境) – MinnuKaAnae
将它传递给'LayoutInflater.from' ...提示:你使用了哪个类的哪个方法? – Selvin
传递上下文作为参数参考Sataym的答案http://stackoverflow.com/a/43518232/3578677 –