问题描述
我的Android(Java)应用程序使用片段在GridLayout中显示产品。 每个产品都是带有两个TextView和一个ImageView的cardview。
我将所有产品存储在mariaDB的Web服务器上。 Fragment的OnCreate方法从服务器获取我的productList。 现在,我想为从服务器返回的所有产品动态创建CardViews。现在,我已经对布局进行了硬编码,但是随着产品数量的变化,必须动态地为产品创建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>
非常感谢您的帮助!
解决方法
您需要将RecyclerView与GridLayoutManager一起使用。 您可以从本文中的简单RecyclerView开始
https://proandroiddev.com/how-to-implement-a-recyclerview-33fd4ff9988e
并在实现RecyclerView之后应用GridLayoutManager。
有关更多信息:
https://developer.android.com/guide/topics/ui/layout/recyclerview