我似乎无法弄清为什么我无法加载任何类型的图像(我尝试过多个URL)。我尝试使用AsyncTask类,但该问题仍未解决。下面列出了我的适配器类,将不胜感激。
我正在尝试为每个RecyclerView条目加载一个图像(在此测试用例中,为同一图像)。当我不尝试设置从URL派生的图像时,会出现默认视图(橙色正方形),但是如果我尝试设置它,则ImageView只是留空。
EntryAdapter
public class EntryAdapter extends RecyclerView.Adapter<EntryAdapter.EntryViewHolder> {
private Entry[] 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 static class EntryViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public String source;
public TextView textContent, title, label, author;
public ImageView thumbnail;
public CardView topCard, mainCard;
public EntryViewHolder(View v) {
super(v);
title = v.findViewById(R.id.title);
textContent = v.findViewById(R.id.textContent);
topCard = v.findViewById(R.id.top_card);
mainCard = v.findViewById(R.id.main_card);
thumbnail = mainCard.findViewById(R.id.thumbnail);
author = mainCard.findViewById(R.id.author);
label = topCard.findViewById(R.id.label);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public EntryAdapter(Entry[] myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public EntryAdapter.EntryViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.entry_item_constraints, parent, false);
EntryViewHolder vh = new EntryViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(EntryViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.title.setText(mDataset[position].getTitle());
holder.label.setText(mDataset[position].getLabel());
holder.textContent.setText(mDataset[position].getTextContent());
holder.thumbnail.setImageDrawable(LoadImageFromWebOperations(mDataset[position].getThumbnail()));
holder.author.setText(mDataset[position].getAuthor());
//new DownloadImageTask(holder.thumbnail).execute(mDataset[position].getThumbnail());
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.length;
}
public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "reddit_thumbnail");
return d;
} catch (Exception e) {
return null;
}
}
}
答案 0 :(得分:3)
尝试使用Glide
在RecyclerView
中显示图像,该图像处理延迟加载以使列表更平滑地滚动。当滚动列表以提高性能和资源消耗时,它也会取消加载操作。
Glide.with(holder.thumbnail.getContext())
.load(mDataset[position].getThumbnail())
.into(holder.thumbnail);
在应用级别 build.gradle:
repositories {
mavenCentral()
google()
}
dependencies {
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
}
您还可以使用placeholder()
方法来指定图像(例如,它可能指向使用资源ID的可绘制对象),以在未下载或无法使用目标图像时显示该图像。