我正在创建一个基于墙纸的 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);
}
}
}