Android Picasso obtains and displays remote images,
Picasso is an open-source Android image cache library of Square. The address is http://square.github.io/picasso/. you can download and save images.
It is easy to use, and only one line of code is required to fully implement asynchronous loading of images:
Picasso. with (view. getContext (). load (newsItems. get (position). getThumb (). into (holder. ivHeader); // Picasso asynchronously loads the preview Image
Images can be loaded without any other operations, and the cache function is provided, and the reuse of images in RecyclerView is also optimized, in addition, it uses a complex image compression algorithm to reduce memory consumption and built-in second-level cache.
The following is an example written by a blogger in the Adapter:
1 package socialnews.linccy.com. socialnews. adapter; 2 3 import android. support. v7.widget. recyclerView; 4 import android. view. layoutInflater; 5 import android. view. view; 6 import android. view. viewGroup; 7 import android. widget. imageView; 8 import android. widget. textView; 9 10 import com. squareup. picasso. picasso; 11 12 import java. util. list; 13 14 import butterknife. bindView; 15 import butterknife. butterKnife; 16 import socialnews.linccy.com. socialnews. utils. newsItem; 17 import socialnews.linccy.com. socialnews. r; 18 19/** 20 * Created by KP on. 21 */22 23 public class NewsItemAdapter extends RecyclerView. adapter <NewsItemAdapter. viewHolder> {24 25 private static List <NewsItem> newsItems; 26 private View view; 27 28 public NewsItemAdapter (List <NewsItem> newsItems) {29 this. newsItems = newsItems; 30} 31 32 @ Override33 public ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {34 view = LayoutInflater. from (parent. getContext ()). inflate (R. layout. recyclerview_cardview_item, parent, false); 35 ViewHolder holder = new ViewHolder (view); 36 return holder; 37} 38 39 @ Override40 public void onBindViewHolder (ViewHolder holder, int position) {41 holder. tvTitle. setText (newsItems. get (position ). getTitle (); 42 holder. tvDetail. setText (newsItems. get (position ). getDescr (); 43 holder. tvTime. setText (newsItems. get (position ). getTime (); 44 Picasso. with (view. getContext ()). load (newsItems. get (position ). getThumb ()). into (holder. ivHeader); // Picasso asynchronously loads the preview image 45} 46 47 @ Override48 public int getItemCount () {49 return newsItems. size (); 50} 51 52 53 public class ViewHolder extends RecyclerView. viewHolder {54 @ BindView (R. id. iv_header) 55 ImageView ivHeader; 56 57 @ BindView (R. id. TV _title) 58 TextView tvTitle; 59 @ BindView (R. id. TV _detail) 60 TextView tvDetail; 61 @ BindView (R. id. TV _time) 62 TextView tvTime; 63 64 public ViewHolder (View itemView) {65 super (itemView); 66 ButterKnife. bind (this, itemView); 67} 68} 69}
: