How to get a context in a recycler view adapter

后端 未结 12 1398
一向
一向 2020-12-02 06:12

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.



        
12条回答
  •  [愿得一人]
    2020-12-02 06:39

    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 :)

提交回复
热议问题