Problem loading images in a Recyclerview with firebase

0

I'm new to android and I'm trying to work with firebase. The problem I have is that when I load the images in a recyclerview it takes a long time to load and I have no idea how to fix it to show the images I'm using glide

I would appreciate your help

public void onBindViewHolder(@NonNull final ViewHolderDatos holder, int position) {
    holder.nombre.setText(listaPlatos.get(position));
   // holder.img.setImageResource(android.R.color.transparent);

    storageRef.child("img/"+listaPlatosI.get(position)+".jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            RequestOptions myOptions = new RequestOptions().fitCenter().override(50, 50).dontAnimate();
            Glide.with(context).load(uri).apply(myOptions).into(holder.img);
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle any errors
        }
    });

}
    
asked by Diana Marcela García Naranjo 08.08.2018 в 07:14
source

1 answer

1

This happens because a request is being made to the database for each iteration of the recyclerview list to get the url of the image in question, so if you have for example 3000 items on your list, there will be 3000 requests to your firebase database.

The normal thing would be that your list of items already contains the images and you only have to load each one in the onBindViewHolder without having to obtain them from the database, remember that the onBindViewHolder method is executed as many times as there are items in your list so there should be no http, heavy code or asynchronous requests in it.

  • The correct code would be the following: Assuming that you have already declared and filled out your list of images.

    public void onBindViewHolder(@NonNull final 
       ViewHolderDatos holder, int position) {
    
        RequestOptions myOptions = new 
        RequestOptions().fitCenter().override(50, 
            50).dontAnimate();
        Glide.with(context).load(miLista.get(position))
            .apply(myOp tions).into(holder.img);
    }
    
answered by 26.08.2018 в 20:48