如何在回收站视图适配器中获取上下文
我试图使用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(); } }
这里有几个选项:
- 将
Context
作为parameter passing给FeedAdapter并将其保留为类字段 - 在需要时使用dependency injection来注入
Context
。 我强烈build议阅读一下。 有一个很好的工具 – Square的Dagger -
从任何
View
对象获取它。 在你的情况下,这可能适合你:holder.pub_image.getContext()
由于
pub_image
是一个ImageView
。
你可以添加全局variables:
private Context context;
然后从这里分配上下文:
@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; }
快乐的Codding 🙂
您可以使用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); }
你可以像这样使用view.getContext()
例
holder.tv_room_name.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(v.getContext(), "", Toast.LENGTH_SHORT).show(); } });
首先全球申报
Context mContext;
通过修改构造函数传递上下文。
public FeedAdapter(List<Post> myDataset, Context context) { mDataset = myDataset; this.mContext = context; }
然后使用mContext
你需要它
创build一个FeedAdapter的构造函数:
Context context; //global public FeedAdapter(Context context) { this.context = context; }
并在活动
FeedAdapter obj = new FeedAdapter(this);
首先添加一个全局variables
Context mContext;
然后改变你的构造函数
public FeedAdapter(Context context, List<Post> myDataset) { mContext = context; mDataset = myDataset; }
在创build适配器时传递你的上下文。
FeedAdapter myAdapter = new FeedAdapter(this,myDataset);
你可以使用这个:
itemView.getContext()