java - 将 Firebase 图像作为可绘制对象检索并显示在回收站视图中
问题描述
我已经实现了 RecyclerView 可以成功显示图像并且现在在我的 Fragment 中一切正常我想从 Firebase 加载图像(图像已经存储在实时数据库中及其 url)这是我的片段代码
public RecyclerView youthRecycler;
public RecyclerView.Adapter recyclerAdapter;
public RecyclerView.LayoutManager recyclerManager;
//i just want to populate this array with drawables from images stored on firebase
int[] imagesArray={R.drawable.youthtable1,R.drawable.youthtable2,R.drawable.youthtable3};
public tableYouthFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_table_youth, container, false);
}
@Override
public void onStart() {
super.onStart();
//all code goes here
init();
}
public void init()
{
youthRecycler=getActivity().findViewById(R.id.youthRecycler);
youthRecycler.setHasFixedSize(true);
recyclerManager=new LinearLayoutManager(getContext());
youthRecycler.setLayoutManager(recyclerManager);
recyclerAdapter =new MyRecyclerAdpater(getActivity(),imagesArray);
youthRecycler.setAdapter(recyclerAdapter);
}
这个问题已经在这里问过,但没有任何经过验证的答案,并且在 kotlin 和 java 答案中的经过验证的答案要求我更改我的 RecyclerView 整个实施。我也跟进了许多关于 glide 库的教程,但不知道是否可以将图像加载为可绘制对象,或者我必须更改我的整个适配器实现
解决方案
你的 recyclerView 应该获取一个 url 列表,你可以在 Glide 中加载它们,为什么你需要将它们加载为 drawable 而 glide 可以将它们加载为 url:
在 onBindViewHolder() 中:
Glide.with(context).load(url).into(holder.imageYouWantToShow);
现在在片段中获取网址:
DatabaseRefernce imagesRef = FirebaseDatabase.getInstance().getReference();
ValueEventListener imagesListner = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot imageUrl: dataSnapshot.getChildren()) {
String image = imageUrl.getValue(String.class);
imagesArray.add(image);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
};
imagesRef.addValueEventListener(imagesListner );
recyclerManager=new LinearLayoutManager(getContext());
youthRecycler.setLayoutManager(recyclerManager);
recyclerAdapter = new MyRecyclerAdpater(this,imagesArray);
youthRecycler.setAdapter(recyclerAdapter);
推荐阅读
- azure - 如何在 MVC 5 应用程序的 azure 自动缩放环境中实现 redis 会话提供程序?
- php - 在动态数据库加载代码中包含静态 html 图像
- r - 使用“for 循环”在 R 中运行一系列线性回归
- c# - 在 C# 中调用 COM 方法会引发无法解释的 COMException,在 C++ 中可以正常工作
- r - 如何使用 For 循环向 data.frame 中的所有行添加常量值
- git - 无法在 github 上推送一些 refs
- javascript - 构造函数的实例可以继承原型桶中未定义的成员吗?
- python - How do I check a data exist or not in text file using python?
- c - 尝试在第二个 2d char 数组中检查一个 2d char 数组中的关键字
- system-verilog - systemverilog comparing two ways to wait signal; 1) @( clock iff condition), 2) while( ! condition) @(clock);