我有一个recyclerview,通过数据绑定填充数据。以前我在Picasso自定义绑定适配器的帮助下填充图像,但现在我正在升级到firebase存储但没有得到代码用recyclerview填充它。请帮助。
答案 0 :(得分:0)
以下是我们存储指向存储的网址的示例,然后使用Glide将其下载到回收站视图中。
// Chat message text can contain a download URL or text
public class ChatMessage {
public String name;
public String message;
public ChatMessage() {
}
public ChatMessage(String name, String message) {
this.name = name;
this.message = message;
}
}
// Use a custom ViewHolder populated from the chat message
public class ChatMessageViewHolder extends RecyclerView.ViewHolder {
private static final String TAG = "ChatMessageViewHolder";
private final Activity activity;
TextView name, message;
ImageView image;
public ChatMessageViewHolder(Activity activity, View itemView) {
super(itemView);
this.activity = activity;
name = (TextView) itemView.findViewById(android.R.id.text1);
message = (TextView) itemView.findViewById(android.R.id.text2);
image = new ImageView(activity);
((ViewGroup)itemView).addView(image);
}
public void bind(ChatMessage chat) {
name.setText(chat.name);
if (chat.message.startsWith("https://firebasestorage.googleapis.com/") || chat.message.startsWith("content://")) {
message.setVisibility(View.INVISIBLE);
image.setVisibility(View.VISIBLE);
// Use Glide (you could use Picaso) to load the image
Glide.with(activity)
.load(chat.message)
.into(image);
}
else {
message.setVisibility(View.VISIBLE);
image.setVisibility(View.GONE);
message.setText(chat.message);
}
}
}
This gist和this video详细说明了如何在完整的应用中执行此操作