【发布时间】:2020-04-19 03:31:22
【问题描述】:
我正在编写一个 Android 应用程序,它在我的 ActivityMain 中包含一个 RecyclerView。每行都有一个 imageButton 和一个 textLabel。在 onBindViewHolder() 方法中,我从 URL 下载图像,并在使用 setImageDrawable 方法后,将图像更改为刚刚下载的图像。
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
holder.textAuthor.setText(posts.get(position).getAuthorName());
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
// Get URL Image from my ArrayList Posts(which contains imageButton and textLabel)
URL url = new URL(posts.get(position).getImageView());
// Get inputStream from URL
InputStream inputStream = (InputStream) url.getContent();
// Create a drawable which contains my downloaded image
Drawable drawable = Drawable.createFromStream(inputStream, null);
// Change the image with the new one
holder.imageButton.setImageDrawable(drawable);
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
我所有的 ImageButton 更改图像都从他们的 URL 正确下载,但问题是它们的大小错误,直到我向下滚动,过了一会儿我用手指在 RecyclerView 内向上滚动。听起来好像我必须刷新我的视图,但我不知道该怎么做。
注意:所有图片都有不同的大小
我给你一些图片来更好地解释我的问题:
When I open my application and it starts to download image from URL
When the download is completed the images have wrong size
If I scroll down and after I scroll up I get the images with the correct size
我的其余代码在这里:MainActivtyMyAdapterPostClass
包含 ImageButton 的 XML 文件:my row of RecyclerView
希望你能一如既往地帮助我,非常感谢!
编辑:我已经修复了使用 Glide 下载图像的问题!
【问题讨论】:
标签: java android android-recyclerview image-resizing android-imagebutton