正在从firebase加载图像(整个文件夹到RecyclerView)



有很多关于如何从firebase中提取图像的帖子,但我仍然不理解。我的代码什么都不做,所以请给我一个示例方法。

我不明白如何使用Glide或Picasso在.with.load.into中插入什么。我的目标是加载整个类别中的所有内容,并将其显示在一个片段中。我的firebase存储结构(我附上json(:

{
"Category1": {
"01": {
"imageLink": "https://firebasestorage.googleapis.com/v0/b/wallex0111.appspot.com/o/photo_2018-10-19_21-38-21.jpg?alt=media&token=f1d29e64-a9ca-4c71-aba7-8144992c835e",
},
"02": {
"imageLink": "https://firebasestorage.googleapis.com/v0/b/wallex0111.appspot.com/o/photo_2018-10-31_10-38-06.jpg?alt=media&token=6889a3b8-3691-41ea-9162-91558ba4181b",
},
"03": {
"imageLink": "https://firebasestorage.googleapis.com/v0/b/wallex0111.appspot.com/o/photo_2018-10-06_14-19-41.jpg?alt=media&token=e75b0096-aaf5-4952-827b-7efb71d6723c",
}
},
"Category2": {
"01": {
"imageLink": "https://firebasestorage.googleapis.com/v0/b/wallex0111.appspot.com/o/photo_2018-10-07_13-55-55.jpg?alt=media&token=bd37d98c-215a-4caf-bf37-d43d78736c31",
},
"02": {
"imageLink": "https://firebasestorage.googleapis.com/v0/b/wallex0111.appspot.com/o/photo_2018-10-31_10-38-06.jpg?alt=media&token=6889a3b8-3691-41ea-9162-91558ba4181b",
}
}

}

碎片

public class AllFrtagment extends Fragment {}

xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.AllFrtagment">
<RelativeLayout
android:id="@+id/relLayyout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/relLayyout1"
android:orientation="vertical"
android:weightSum="100">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="60">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="1dp"
android:layout_weight="40"
android:gravity="center"
android:horizontalSpacing="1dp"
android:numColumns="5"
android:stretchMode="none"
android:verticalSpacing="1dp"></GridView>
<!--I think that using GridView is all I need...? Without additional ImageView-->
<ImageView
android:id="@+id/galleryImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>

您不需要那么多嵌套视图(假设您只想加载图像(。一个RecyclerView就足够了

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.AllFrtagment">
<RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>

将json解析为一个模型类。创建一个recyclerview,并将您的imageLinks列表传递给recyclerview适配器。并使用任何图像库来显示您的图像。

让我们现在谈谈Glide。

GlideApp.with(context) //with(), takes your context
.load(imageUrl) //load(), takes your image url
.into(galleryImageView); // Your ImageView is where the image will be shown.

使用Picasso

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
Picasso.get().load(city.getImage())
.placeholder(R.drawable.ic_launcher_foreground)
.into(holder.image);
  • 您可以在Github上找到完整的示例/CityPreview应用程序

最新更新