英文:
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:
Here's how I implemented the layout:
<?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>
Also, here's the 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>
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论