Images showing slow from firebase realtime database

174 Views Asked by At

I am creating an Android app which is based on wallpaper. I have different categories in my app that shows images and after clicking on particular image it will show full image. The problem is when I click on any categories the images are shown are very slow. I am retrieving images from firebase realtime database. I am showing images in grid layout in two columns. I have also compress all the images then to the process of showing images is very slow.

This is one of the category code.

public class AnimalWallpaper extends AppCompatActivity {
private RecyclerView recyclerView;
private ProgressBar progressBar;
private DatabaseReference reference;
private ArrayList<String> list;
private WallpaperAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_animal_wallpaper);

    reference = FirebaseDatabase.getInstance().getReference().child("animal");

    recyclerView = findViewById(R.id.recyclerViewAnimal);
    progressBar = findViewById(R.id.progressBarAnimal);

    getData();
}

private void getData() {
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {

            progressBar.setVisibility(View.GONE);
            list = new ArrayList<>();

            for (DataSnapshot shot : snapshot.getChildren()) {

                String data = shot.getValue().toString();
                list.add(data);
            }

            recyclerView.setLayoutManager(new GridLayoutManager(AnimalWallpaper.this, 2));
            adapter  = new WallpaperAdapter(list, AnimalWallpaper.this);
            recyclerView.setAdapter(adapter);
            progressBar.setVisibility(View.GONE);


        }

        @Override
        public void onCancelled(@NonNull @NotNull DatabaseError error) {

            progressBar.setVisibility(View.GONE);
            Toast.makeText(AnimalWallpaper.this, "Error : "+error.getMessage() , Toast.LENGTH_SHORT).show();
        }
    });
}

}

This is wallpaperadapter class code.

public class WallpaperAdapter extends RecyclerView.Adapter<WallpaperAdapter.WallpaperViewHolder> {

private ArrayList<String> list;
private Context context;

public WallpaperAdapter(ArrayList<String> list, Context context) {
    this.list = list;
    this.context = context;
}

@NonNull
@org.jetbrains.annotations.NotNull
@Override
public WallpaperViewHolder onCreateViewHolder(@NonNull @org.jetbrains.annotations.NotNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.custom_image_layout, parent, false );

    return new WallpaperViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull @org.jetbrains.annotations.NotNull WallpaperAdapter.WallpaperViewHolder holder, int position) {

    Glide.with(context).load(list.get(position)).into(holder.imageView);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(context, FullImageActivity.class);
            intent.putExtra("images", list.get(position));
            context.startActivity(intent);
        }
    });
}

@Override
public int getItemCount() {
    return list.size();
}

public class WallpaperViewHolder extends RecyclerView.ViewHolder {

    ImageView imageView;

    public WallpaperViewHolder(@NonNull @NotNull View itemView) {
        super(itemView);

        imageView = itemView.findViewById(R.id.item_image);
    }
}

}
0

There are 0 best solutions below