Java 如何在回收器视图适配器中获取上下文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32136973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a context in a recycler view adapter
提问by Stranger B.
I'm trying to use picasso library to be able to load url to imageView, but I'm not able to get the context
to use the picasso library correctly.
我正在尝试使用 picasso 库来将 url 加载到 imageView,但我无法context
正确使用 picasso 库。
public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.ViewHolder> {
private List<Post> mDataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView txtHeader;
public ImageView pub_image;
public ViewHolder(View v) {
super(v);
txtHeader = (TextView) v.findViewById(R.id.firstline);
pub_image = (ImageView) v.findViewById(R.id.imageView);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public FeedAdapter(List<Post> myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public FeedAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.feedholder, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.txtHeader.setText(mDataset.get(position).getPost_text());
Picasso.with(this.context).load("http://i.imgur.com/DvpvklR.png").into(holder.pub_image);
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.size();
}
}
采纳答案by pelotasplus
You have a few options here:
您在这里有几个选择:
- Pass
Context
as an argument to FeedAdapter and keep it as class field - Use dependency injection to inject
Context
when you need it. I strongly suggest reading about it. There is a great tool for that -- Daggerby Square Get it from any
View
object. In your case this might work for you:holder.pub_image.getContext()
As
pub_image
is aImageView
.
Context
作为参数传递给 FeedAdapter 并将其保留为类字段Context
需要时使用依赖注入进行注入。我强烈建议阅读它。有一个很好的工具——Daggerby Square从任何
View
对象获取它。在您的情况下,这可能对您有用:holder.pub_image.getContext()
就像
pub_image
一个ImageView
.
回答by blockwala
Create a constructor of FeedAdapter :
创建 FeedAdapter 的构造函数:
Context context; //global
public FeedAdapter(Context context)
{
this.context = context;
}
and in Activity
并在活动中
FeedAdapter obj = new FeedAdapter(this);
回答by Blue_Alien
First globally declare
首先全局声明
Context mContext;
Context mContext;
pass context with the constructor, by modifying it.
通过修改构造函数传递上下文。
public FeedAdapter(List<Post> myDataset, Context context) {
mDataset = myDataset;
this.mContext = context;
}
then use the mContext
whereever you need it
然后在mContext
任何需要的地方使用
回答by AKT
First add a global variable
首先添加一个全局变量
Context mContext;
Then change your constructor to this
然后将您的构造函数更改为此
public FeedAdapter(Context context, List<Post> myDataset) {
mContext = context;
mDataset = myDataset;
}
The pass your context when creating the adapter.
创建适配器时传递您的上下文。
FeedAdapter myAdapter = new FeedAdapter(this,myDataset);
回答by Farhad Navayazdan
You can add global variable:
您可以添加全局变量:
private Context context;
then assign the context from here:
然后从这里分配上下文:
@Override
public FeedAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
// create a new view
View v=LayoutInflater.from(parent.getContext()).inflate(R.layout.feedholder, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
// set the Context here
context = parent.getContext();
return vh;
}
Happy Codding :)
快乐编码:)
回答by Ahmad Aghazadeh
You can use pub_image context (holder.pub_image.getContext()
) :
您可以使用 pub_image 上下文 ( holder.pub_image.getContext()
) :
@Override
public void onBindViewHolder(ViewHolder ViewHolder, int position) {
holder.txtHeader.setText(mDataset.get(position).getPost_text());
Picasso.with(holder.pub_image.getContext()).load("http://i.imgur.com/DvpvklR.png").into(holder.pub_image);
}
回答by Arpit Patel
You can use like this view.getContext()
您可以像这样使用view.getContext()
Example
例子
holder.tv_room_name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "", Toast.LENGTH_SHORT).show();
}
});
回答by u7962780
you can use this:
你可以使用这个:
itemView.getContext()
回答by Maciej Beimcik
Short answer:
简短的回答:
Context context;
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
context = recyclerView.getContext();
}
Explanation why other answers are not great:
解释为什么其他答案不是很好:
- Passing
Context
to the adapter is completely unnecessary, sinceRecyclerView
you can access it from inside the class - Obtaining
Context
atViewHolder
level means that you do it every time you bind or create aViewHolder
. You duplicate operations. - I don't think you need to worry about any memory leak. If your adapter lingers outside your
Activity
lifespan (which would be weird) then you already have a leak.
- 传递
Context
给适配器是完全没有必要的,因为RecyclerView
您可以从类内部访问它 Context
在ViewHolder
级别获取意味着您每次绑定或创建ViewHolder
. 您重复操作。- 我认为您无需担心任何内存泄漏。如果您的适配器在您的
Activity
使用寿命之外徘徊(这很奇怪),那么您已经泄漏了。
回答by Marcos vitouley
You can define:
您可以定义:
Context ctx;
And on onCreate
initialise ctx
to:
并onCreate
初始化ctx
为:
ctx=parent.getContext();
Note: Parent is a ViewGroup.
注意:Parent 是一个 ViewGroup。