我有一个循环视图,它加载存储在Firebase存储中的不同图像。当我滚动我的循环视图时,它每次都加载相同的图像(就像循环视图的定义一样)。
如何下载这些图像一次,并附加到我的循环视图,使它不应该重新加载时,滚动?
我试过这样做。
public void onBindViewHolder(final TodaysBdayViewHolder holder, int position)
{
storageReference=FirebaseStorage.getInstance().getReference().child("profiles/"+contacts.get(position)+".jpg");
final File localFile=File.createTempFile("profile_pic","jpeg",new File(context.getExternalFilesDir("null").getAbsolutePath()));
storageReference.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Log.i("app","FinishIntro Img loaded");
Bitmap bitmap= BitmapFactory.decodeFile(localFile.getAbsolutePath());
holder.FriendPhoto.setImageBitmap(bitmap);
}
}).addOnFailureListener(new OnFailureListener() {
public void onFailure(@NonNull Exception e) {
}
});
}发布于 2020-04-22 07:04:19
您可以使用滑翔缓存图像并在图像视图中显示它。
public void onBindViewHolder(final TodaysBdayViewHolder holder, int position)
{
storageReference = FirebaseStorage.getInstance().getReference().child("profiles/"+contacts.get(position)+".jpg");
// get the download URL
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
public void onSuccess(Uri uri) {
// load and cache with glide
Glide.with(context)
.load(uri)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.FriendPhoto);
}
}).addOnFailureListener(new OnFailureListener() {
public void onFailure(@NonNull Exception e) {
}
});
}发布于 2020-04-22 06:54:41
您正在从文件中自己解码位图,这不是很有效。相反,我建议使用一个库来高效地完成类似毕加索或滑行这样的事情。
毕加索方法:
添加到gradle:
implementation 'com.squareup.picasso:picasso:2.71828'并在加载时执行此操作:
//the file
final File localFile=File.createTempFile("profile_pic","jpeg",new File(context.getExternalFilesDir("null").getAbsolutePath()));
//the reference
storageReference=FirebaseStorage.getInstance().getReference().child("profiles/"+contacts.get(position)+".jpg");
//the call
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// use this uri in picasso call into imageview
Picasso.get().load(uri.toString()).into(holder.FriendPhoto);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
}
});https://stackoverflow.com/questions/61358724
复制相似问题