从 ViewHolder class 在 Glide 中传递上下文?
Passing context in Glide from a ViewHolder class?
我试图在 with()
中传递上下文,但是 "this" 不起作用,getContext()
、getActivity()
也不起作用。我的 class 是一个扩展 RecycleView
的 ViewHolder。我如何获取上下文?
protected void bind(I item, @Nullable OnContactClickListener<I> listener) {
Author author = item.getContact().getAuthor();
Glide.with(this /* context */)
.using(new FirebaseImageLoader())
.load(storageRef.child("/"+author.getName()+"/pic.jpg"))
.error(new IdenticonDrawable(author.getId().getBytes()))
.into(avatar);
String contactName = author.getName();
name.setText(contactName);
来自这一行
name.setText(contactName);
我可以说名字是TextView
。您可以使用 view.getContext()
从视图中获取 Context
。 view.getContext()
returns 视图在 运行 中的上下文。
Glide.with(name.getContext())
你可以这样试试:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private final Context context;
public MyAdapter(Context context) {
this.context = context;
}
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(this.context).inflate(R.layout.frames_adapter_item, parent, false));
}
public void onBindViewHolder(@NonNull final ViewHolder holder, @SuppressLint("RecyclerView") int position) {
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
holder.progress_bar.setVisibility(View.GONE);
Glide.with(getApplicationContext()).load(arrayOfFile.get(position)).asBitmap().into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
if (resource != null) {
imageView.setImageBitmap(resource);
}
}
});
}
public int getItemCount() {
return 1;
}
class ViewHolder extends RecyclerView.ViewHolder {
final ImageView imageView;
final ProgressBar progress_bar;
ViewHolder(View itemView) {
super(itemView);
this.imageView = itemView.findViewById(R.id.imageView);
this.progress_bar = itemView.findViewById(R.id.progress_bar);
}
}
}
如此简单地获取 avtar 视图的上下文。
protected void bind(I item, @Nullable OnContactClickListener<I> listener) {
Author author = item.getContact().getAuthor();
Glide.with(avatar.getContext())
.using(new FirebaseImageLoader())
.load(storageRef.child("/"+author.getName()+"/pic.jpg"))
.error(new IdenticonDrawable(author.getId().getBytes()))
.into(avatar);
String contactName = author.getName();
name.setText(contactName);
您可以使用
Glide.with(itemview)
.using(new FirebaseImageLoader())
.load(storageRef.child("/"+author.getName()+"/pic.jpg"))
.error(new IdenticonDrawable(author.getId().getBytes()))
.into(avatar);
我试图在 with()
中传递上下文,但是 "this" 不起作用,getContext()
、getActivity()
也不起作用。我的 class 是一个扩展 RecycleView
的 ViewHolder。我如何获取上下文?
protected void bind(I item, @Nullable OnContactClickListener<I> listener) {
Author author = item.getContact().getAuthor();
Glide.with(this /* context */)
.using(new FirebaseImageLoader())
.load(storageRef.child("/"+author.getName()+"/pic.jpg"))
.error(new IdenticonDrawable(author.getId().getBytes()))
.into(avatar);
String contactName = author.getName();
name.setText(contactName);
来自这一行
name.setText(contactName);
我可以说名字是TextView
。您可以使用 view.getContext()
从视图中获取 Context
。 view.getContext()
returns 视图在 运行 中的上下文。
Glide.with(name.getContext())
你可以这样试试:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private final Context context;
public MyAdapter(Context context) {
this.context = context;
}
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(this.context).inflate(R.layout.frames_adapter_item, parent, false));
}
public void onBindViewHolder(@NonNull final ViewHolder holder, @SuppressLint("RecyclerView") int position) {
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
holder.progress_bar.setVisibility(View.GONE);
Glide.with(getApplicationContext()).load(arrayOfFile.get(position)).asBitmap().into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
if (resource != null) {
imageView.setImageBitmap(resource);
}
}
});
}
public int getItemCount() {
return 1;
}
class ViewHolder extends RecyclerView.ViewHolder {
final ImageView imageView;
final ProgressBar progress_bar;
ViewHolder(View itemView) {
super(itemView);
this.imageView = itemView.findViewById(R.id.imageView);
this.progress_bar = itemView.findViewById(R.id.progress_bar);
}
}
}
如此简单地获取 avtar 视图的上下文。
protected void bind(I item, @Nullable OnContactClickListener<I> listener) {
Author author = item.getContact().getAuthor();
Glide.with(avatar.getContext())
.using(new FirebaseImageLoader())
.load(storageRef.child("/"+author.getName()+"/pic.jpg"))
.error(new IdenticonDrawable(author.getId().getBytes()))
.into(avatar);
String contactName = author.getName();
name.setText(contactName);
您可以使用
Glide.with(itemview)
.using(new FirebaseImageLoader())
.load(storageRef.child("/"+author.getName()+"/pic.jpg"))
.error(new IdenticonDrawable(author.getId().getBytes()))
.into(avatar);