ScrollView Glide 中的 Android ListView 可加载 FireBase 中的图像,而不是始终



我在ScrollView中有一个列表视图,它将显示图像和文本。文本完美显示。然而,图像不是。如果我慢慢滚动列表 - 所有图像都会完美加载而不会失败,但如果我走得非常快,它们永远不会被加载。我认为这就是我调用该方法的设置方式,但这是我在 Android 中的第一天。以下是资源、布局和类。

我如何在列表中添加文本和图像视图:

<?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的工作原理。

尝试,将图像视图作为参数提供给getImage而不是全局变量。例如:

    // inside getView
    ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
    getImages(position, imageView);
    // GetImage
    public void getImages(Integer position, ImageView im) {
       ....
    }

首先,最好在列表中实现ViewHolder模式或使用RecyclerView。在您当前的情况下,您一遍又一遍地夸大视图,这不是必需的,如果视图太复杂,可能会降低应用程序的性能。您可以在此处阅读有关视图持有人模式的更多信息

 @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;
}

最新更新