动态添加多个 CardView 到 Fragment xml

Add multiple CardView dynamically to Fragment xml

我的 Android (Java) 应用程序使用片段在 GridLayout 中显示产品。 每个产品都是一个带有两个 TextView 和一个 ImageView 的卡片视图。

我的所有产品都存储在 mariaDB 的网络服务器上。 Fragment 的 OnCreate 方法从服务器获取我的 productList。 现在我想为从服务器返回的所有产品动态创建 CardView。目前,我已经对布局进行了硬编码,但由于产品数量可能会发生变化,因此有必要为产品动态创建 CardViews。

如何在 Java 中实现此目的?

片段看起来是这样的:

以下是我实现布局的方式:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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.DrinksFragment">

<androidx.gridlayout.widget.GridLayout
    android:id="@+id/mainGrid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="14dp"
    app:alignmentMode="alignMargins"
    app:columnCount="3"
    app:columnOrderPreserved="false"
    app:rowCount="3">

    <androidx.cardview.widget.CardView
        android:id="@+id/cV_water"
        style="@style/ProductCardStyle">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="16dp"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/iV_water"
                style="@style/ImageStyle"
                android:contentDescription="@string/ic_water"
                app:layout_constraintBottom_toTopOf="@+id/tV_counterWater"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/tV_descWater"
                app:srcCompat="@drawable/ic_water_bottle" />

            <TextView
                android:id="@+id/tV_descWater"
                style="@style/TextStyle"
                android:text="@string/product_water"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/tV_counterWater"
                style="@style/TextStyle"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>
</androidx.gridlayout.widget.GridLayout>
</FrameLayout>

此外,这是 ProductCardStyle:

<style name = "ProductCardStyle">
    <item name = "cardBackgroundColor">@color/colorPrimary</item>
    <item name = "android:layout_width">0dp</item>
    <item name = "android:layout_height">0dp</item>
    <item name = "android:layout_marginLeft">2dp</item>
    <item name = "android:layout_marginRight">2dp</item>
    <item name = "android:layout_marginBottom">2dp</item>
    <item name = "android:layout_marginTop">2dp</item>
    <item name = "cardCornerRadius">4dp</item>
    <item name = "cardElevation">8dp</item>
    <item name = "layout_columnWeight">1</item>
    <item name = "layout_rowWeight">1</item>
</style>

非常感谢任何帮助!

您需要将 RecyclerView 与 GridLayoutManager 一起使用。 您可以像本文中那样从简单的 RecyclerView 开始

https://proandroiddev.com/how-to-implement-a-recyclerview-33fd4ff9988e

并在 RecyclerView 实现后应用 GridLayoutManager。

更多信息:

https://developer.android.com/guide/topics/ui/layout/recyclerview