How to get a context in a recycler view adapter

后端 未结 12 1401
一向
一向 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:49

    You have a few options here:

    1. Pass Context as an argument to FeedAdapter and keep it as class field
    2. Use dependency injection to inject Context when you need it. I strongly suggest reading about it. There is a great tool for that -- Dagger by Square
    3. Get it from any View object. In your case this might work for you:

      holder.pub_image.getContext()

      As pub_image is a ImageView.

提交回复
热议问题