我尝试缓存从FireStorage获得的图像并在recyclerview中显示通过使用(毕加索或滑行),但对我不起作用****我想缓存图像并在设备离线时使用它]]
@NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_main, parent, false); return new ViewHolder(v); } class ViewHolder extends RecyclerView.ViewHolder { private AppCompatImageView imageViewIv; private MaterialTextView nameTv; ViewHolder(@NonNull View itemView) { super(itemView); imageViewIv = itemView.findViewById(R.id.iv_cardview); nameTv = itemView.findViewById(R.id.tv_cardview_name); } void bind(StudentModel model) { nameTv.setText(model.getName()); StorageReference reference = FirebaseStorage.getInstance().getReference().child("student photo/" + model.getName()); reference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { @Override public void onSuccess(Uri uri) { Glide.with(context).load(uri).fitCenter().centerCrop().into(imageViewIv); } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Toast.makeText(context, "failed to load images", Toast.LENGTH_SHORT).show(); } }); } }
这是保存recyclerview适配器的片段类,并且布局包含recycleview
public class StudentFragment extends Fragment { private StudentInfoFireAdapter adapter; public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = inflater.inflate(R.layout.fragment_recyclerview, container, false); RecyclerView recyclerView = root.findViewById(R.id.rv_fragment); CollectionReference collection = FirebaseFirestore.getInstance().collection("student"); Query query = collection.orderBy("dob", Query.Direction.ASCENDING); FirestoreRecyclerOptions<StudentModel> options = new FirestoreRecyclerOptions.Builder<StudentModel>() .setQuery(query, StudentModel.class) .build(); adapter = new StudentInfoFireAdapter(options, getContext()); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); recyclerView.setAdapter(adapter); FloatingActionButton fab = root.findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(getActivity(), AddStudent.class); startActivity(intent); } }); return root; } @Override public void onStart() { super.onStart(); adapter.startListening(); } @Override public void onStop() { super.onStop(); adapter.stopListening(); }
}
我尝试缓存我从FireStorage获得的图像,并使用(Picasso或glide)在recyclerview中显示,但是对我不起作用****我想缓存图像并在设备运行时使用它...
这是您可以使用Glide进行的操作: