【问题标题】:Android ListView inside ScrollView Glide to load images off FireBase not loading alwaysScrollView Glide 中的 Android ListView 从 FireBase 加载图像并不总是加载
【发布时间】: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


【解决方案1】:

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

    // inside getView
    ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
    getImages(position, imageView);

    // GetImage
    public void getImages(Integer position, ImageView im) {
       ....
    }

【讨论】:

  • @Torewin 快乐编码
【解决方案2】:

首先,最好在您的列表中实现 ViewHolder 模式或使用 RecyclerView。在您当前的情况下,您一遍又一遍地膨胀视图,这是不必要的,如果视图太复杂,可能会降低应用程序的性能。你可以阅读更多关于 ViewHolder 模式here

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

【讨论】:

  • 感谢您提供此信息。当我回去重写和构建所有内容时,我将使用此方法 - 现在我只是得到一个工作模型。
猜你喜欢
  • 2020-06-28
  • 2018-07-05
  • 2019-10-13
  • 1970-01-01
  • 2016-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多