[英]Android ListView inside ScrollView Glide to load images off FireBase not loading always
我在ScrollView中有一个ListView,它将显示图像和文本。 文本显示完美。 但是图像不是。 如果我缓慢滚动列表,则所有图像都会完美加载而不会失败,但是如果我走得很快,它们就永远不会加载。 我认为这是我设置调用方法的地方,但这是我在Android中的第一天。 以下是资源,布局和类。
我如何在列表中添加Text和ImageView:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<ImageView
android:id="@+id/img"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="50dp" />
</TableRow>
</TableLayout>
与之相伴的课程:
public class someClass extends ArrayAdapter<String>{
private final Activity context;
private final ArrayList<String> web;
ImageView imageView;
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReference();
final ArrayList<Uri> uriList = new ArrayList<>();
public galveon_character_creation_spelllist(Activity context,
ArrayList<String> web) {
super(context, R.layout.someClass, web);
this.context = context;
this.web = web;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.someClass, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web.get(position));
getImages(position);
return rowView;
}
public void getImages(Integer position) {
storageRef.child("FolderRef/" + web.get(position) + ".png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
//imageView.setImageURI(null);
//imageView.setImageURI(uri);
Glide.with(getContext())
.load(uri) // the uri you got from Firebase
.placeholder(R.drawable.unknown) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url.
.diskCacheStrategy(DiskCacheStrategy.ALL) //using to load into cache then second time it will load fast.
.animate(R.anim.fade_in) // when image (url) will be loaded by glide then this face in animation help to replace url image in the place of placeHolder (default) image.
.fitCenter()//this method help to fit image into center of your ImageView
.into(imageView); //Your imageView variable
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
imageView.setImageResource(R.drawable.unknown);
}
});
}
}
web
只是与图片名称对齐的文本。 同样,当活动加载列表时,看到的所有“单元格”都不会加载,但是当您缓慢向下滚动并向上备份图像时,就会出现(如果缓慢滚动)。
如果我需要发布更多信息,我可以。 仍在学习Android的工作方式。
尝试将imageview作为参数而不是全局变量作为getImage的参数。 例如:
// inside getView
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
getImages(position, imageView);
// GetImage
public void getImages(Integer position, ImageView im) {
....
}
首先,最好在清单中实现ViewHolder模式或使用RecyclerView。 在当前情况下,您需要一遍又一遍地扩大视图的视图,这是不必要的,如果视图过于复杂,则会降低应用程序的性能。 您可以在此处阅读有关ViewHolder模式的更多信息
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
//Inflate the view once if the view is null
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.someClass, null, true);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.txt)
holder.imageView = (ImageView) convertView.findViewById(R.id.img);
convertView.setTag(holder);
}
else {
//retrieve the reference of the inflated view
holder = (ViewHolder) convertView.getTag();
}
holder.txtTitle.setText(web.get(position));
getImages(holder.imageView, position);
return convertView;
}
public void getImages(ImageView imageView, int position) {
storageRef.child("FolderRef/" + web.get(position) + ".png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
//imageView.setImageURI(null);
//imageView.setImageURI(uri);
Glide.with(getContext())
.load(uri) // the uri you got from Firebase
.placeholder(R.drawable.unknown) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url.
.diskCacheStrategy(DiskCacheStrategy.ALL) //using to load into cache then second time it will load fast.
.animate(R.anim.fade_in) // when image (url) will be loaded by glide then this face in animation help to replace url image in the place of placeHolder (default) image.
.fitCenter()//this method help to fit image into center of your ImageView
.into(imageView); //Your imageView variable
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
imageView.setImageResource(R.drawable.unknown);
}
});
}
static class ViewHolder {
TextView txtTitle;
ImageView imageView;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.