Java 将多个CardView动态添加到片段xml

Java 将多个CardView动态添加到片段xml,java,android,xml,cardview,Java,Android,Xml,Cardview,我的Android(Java)应用程序使用片段在网格布局中显示产品。 每个产品都是一个具有两个文本视图和一个图像视图的cardview 我的所有产品都存储在mariaDB的Web服务器上。Fragment的OnCreate方法从服务器获取我的productList。 现在,我想为从服务器返回的所有产品动态创建CardView。目前,我已经对布局进行了硬编码,但随着产品数量的变化,有必要动态创建产品的CardView 如何在Java中实现这一点 这是片段的外观: 下面是我如何实现布局的: <

我的Android(Java)应用程序使用片段在网格布局中显示产品。 每个产品都是一个具有两个文本视图和一个图像视图的cardview

我的所有产品都存储在mariaDB的Web服务器上。Fragment的OnCreate方法从服务器获取我的productList。 现在,我想为从服务器返回的所有产品动态创建CardView。目前,我已经对布局进行了硬编码,但随着产品数量的变化,有必要动态创建产品的CardView

如何在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>

@颜色/原色
0dp
0dp
2dp
2dp
2dp
2dp
4dp
8dp
1.
1.

非常感谢您的帮助

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

并在实现RecyclerView后应用GridLayoutManager

有关更多信息:


@Gordon.f你能用这个答案解决这个问题吗?如果是这样,请记住接受它。这会向其他用户显示您已经找到了解决方案。