动态向Fragment的XML中添加多个CardView。

huangapple go评论85阅读模式
英文:

Add multiple CardView dynamically to Fragment xml

问题

以下是翻译好的内容:

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

我将所有产品存储在mariaDB中的Web服务器上。片段(Fragment)的OnCreate方法从服务器获取我的产品列表。
现在,我想动态地为从服务器返回的所有产品创建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>

如果有任何帮助,将不胜感激!

英文:

My Android (Java) App uses Fragments to display products in a GridLayout.
Each product is a cardview with two TextViews as well as an ImageView.

I have all my products stored on a webserver in mariaDB. Fragment's OnCreate method gets my productList from server.
Now I want to dynamically create CardViews for all products returned from the server. For now, I have hardcoded the layout, but as the number of products can change, it's neccesary to create CardViews for the products dynamically.

How can I achieve this in Java?

This is how the Fragment looks:

动态向Fragment的XML中添加多个CardView。

Here's how I implemented the layout:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;FrameLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.fragments.DrinksFragment&quot;&gt;
&lt;androidx.gridlayout.widget.GridLayout
android:id=&quot;@+id/mainGrid&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:padding=&quot;14dp&quot;
app:alignmentMode=&quot;alignMargins&quot;
app:columnCount=&quot;3&quot;
app:columnOrderPreserved=&quot;false&quot;
app:rowCount=&quot;3&quot;&gt;
&lt;androidx.cardview.widget.CardView
android:id=&quot;@+id/cV_water&quot;
style=&quot;@style/ProductCardStyle&quot;&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_gravity=&quot;center_horizontal|center_vertical&quot;
android:layout_margin=&quot;16dp&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/iV_water&quot;
style=&quot;@style/ImageStyle&quot;
android:contentDescription=&quot;@string/ic_water&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/tV_counterWater&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/tV_descWater&quot;
app:srcCompat=&quot;@drawable/ic_water_bottle&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/tV_descWater&quot;
style=&quot;@style/TextStyle&quot;
android:text=&quot;@string/product_water&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/tV_counterWater&quot;
style=&quot;@style/TextStyle&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&lt;/androidx.cardview.widget.CardView&gt;
&lt;/androidx.gridlayout.widget.GridLayout&gt;
&lt;/FrameLayout&gt;

Also, here's the ProductCardStyle:

&lt;style name = &quot;ProductCardStyle&quot;&gt;
&lt;item name = &quot;cardBackgroundColor&quot;&gt;@color/colorPrimary&lt;/item&gt;
&lt;item name = &quot;android:layout_width&quot;&gt;0dp&lt;/item&gt;
&lt;item name = &quot;android:layout_height&quot;&gt;0dp&lt;/item&gt;
&lt;item name = &quot;android:layout_marginLeft&quot;&gt;2dp&lt;/item&gt;
&lt;item name = &quot;android:layout_marginRight&quot;&gt;2dp&lt;/item&gt;
&lt;item name = &quot;android:layout_marginBottom&quot;&gt;2dp&lt;/item&gt;
&lt;item name = &quot;android:layout_marginTop&quot;&gt;2dp&lt;/item&gt;
&lt;item name = &quot;cardCornerRadius&quot;&gt;4dp&lt;/item&gt;
&lt;item name = &quot;cardElevation&quot;&gt;8dp&lt;/item&gt;
&lt;item name = &quot;layout_columnWeight&quot;&gt;1&lt;/item&gt;
&lt;item name = &quot;layout_rowWeight&quot;&gt;1&lt;/item&gt;
&lt;/style&gt;

Would appreciate any help!

答案1

得分: 0

你需要使用带有GridLayoutManager的RecyclerView。你可以从类似这篇文章中的简单RecyclerView开始:

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

在实现RecyclerView之后,可以应用GridLayoutManager。

更多信息,请参考:

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

英文:

You need to use RecyclerView with GridLayoutManager.
You can begin with simple RecyclerView like in this article
> https://proandroiddev.com/how-to-implement-a-recyclerview-33fd4ff9988e

And apply GridLayoutManager after RecyclerView implemented.

For more info:

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

huangapple
  • 本文由 发表于 2020年9月14日 17:18:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63881451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定