【发布时间】:2017-08-15 12:51:03
【问题描述】:
我在 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 的工作原理。
【问题讨论】:
-
当listview自己管理scolling时,为什么要把ListView放在scrollview里面?你试过 .diskCacheStrategy(DiskCacheStrategy.SOURCE); 吗?
-
使用 RecyclerView 代替 ListView 以获得更好的结果,或者如果您只想使用 ListView,请在列表适配器类中使用 ViewHolder。
标签: android listview firebase firebase-storage android-glide