У меня реализована загрузка картинок в адаптере с помощью библиотеки Picasso, картинки имеют большой размер, при скролле вверх или вниз, видимо из-за того, что карточки пересоздаются, происходит опять подгрузка картинок, в итоге все это выглядит не очень красиво, как можно от этого уйти? Можно ли ограничить количество пересоздаваемых карточек? Код адаптера:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private List<News> news;
private String URL = null;
private Context context;
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public TextView newsDate;
public TextView summary;
public ImageView img_android;
public ViewHolder(View v) {
super(v);
title = (TextView) v.findViewById(R.id.tvTitleItem);
summary = (TextView) v.findViewById(R.id.tvDescriptionItem);
newsDate = (TextView) v.findViewById(R.id.date);
img_android = (ImageView)v.findViewById(R.id.img_android);
}
}
public RecyclerAdapter(List<News> news, Context context) {
this.news = news;
this.context = context;
}
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_view, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Picasso p = new Picasso.Builder(context)
.memoryCache(new LruCache(240000))
.build();
holder.title.setText(news.get(position).getSummary());
holder.summary.setText(news.get(position).getTitle());
holder.newsDate.setText(getDate(news.get(position).getNewsDate()));
p.with(context)
.load(news.get(position).getImageUrl())
.into(holder.img_android);
}
private String getDate(long time) {
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy hh:mm");
String dateString = formatter.format(new Date(time));
String date = "" + dateString;
return date;
}
}
Вам либо нужны картинки с меньшим разрешением, либо используйте fit()
Picasso.with(context)
.load(news.get(position).getImageUrl())
.fit()
.centerCrop()
.into(holder.img_android);
Сборка персонального компьютера от Artline: умный выбор для современных пользователей