【问题标题】:Add multiple CardView dynamically to Fragment xml将多个 CardView 动态添加到 Fragment xml
【发布时间】:2021-01-01 00:10:08
【问题描述】:

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

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

不胜感激!

【问题讨论】:

    标签: java android xml cardview


    【解决方案1】:

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

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

    并在 RecyclerView 实现后应用 GridLayoutManager。

    更多信息:

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

    【讨论】:

    • @Gordon.f 你能用这个答案来解决这个问题吗?如果是这样,请记住接受它。这会向其他用户显示您找到了解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    相关资源
    最近更新 更多