图像显示来自 firebase 实时数据库的缓慢

问题描述

我正在创建一个基于墙纸的 Android 应用程序。我的应用程序中有不同的类别显示图像,单击特定图像后,它将显示完整图像。问题是当我点击任何类别时,显示的图像非常慢。我正在从 firebase 实时数据库中检索图像。我在两列中以网格布局显示图像。我也压缩了所有的图像然后显示图像的过程很慢。

这是类别代码之一。

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();
        }
    });
}

}

这是壁纸适配器类代码

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

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...