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

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

Add multiple CardView dynamically to Fragment xml

问题

以下是翻译好的内容:

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

我将所有产品存储在mariaDB中的Web服务器上。片段(Fragment)的OnCreate方法从服务器获取我的产品列表。
现在,我想动态地为从服务器返回的所有产品创建CardViews。目前,我已经硬编码了布局,但由于产品数量可能会改变,因此需要动态地创建产品的CardViews。

我如何在Java中实现这一点?

这是片段的外观:

(图片已省略)

以下是我实现布局的方式:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".fragments.DrinksFragment">
  8. <androidx.gridlayout.widget.GridLayout
  9. android:id="@+id/mainGrid"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:padding="14dp"
  13. app:alignmentMode="alignMargins"
  14. app:columnCount="3"
  15. app:columnOrderPreserved="false"
  16. app:rowCount="3">
  17. <androidx.cardview.widget.CardView
  18. android:id="@+id/cV_water"
  19. style="@style/ProductCardStyle">
  20. <androidx.constraintlayout.widget.ConstraintLayout
  21. android:layout_width="match_parent"
  22. android:layout_height="match_parent"
  23. android:layout_gravity="center_horizontal|center_vertical"
  24. android:layout_margin="16dp"
  25. android:orientation="vertical">
  26. <ImageView
  27. android:id="@+id/iV_water"
  28. style="@style/ImageStyle"
  29. android:contentDescription="@string/ic_water"
  30. app:layout_constraintBottom_toTopOf="@+id/tV_counterWater"
  31. app:layout_constraintEnd_toEndOf="parent"
  32. app:layout_constraintStart_toStartOf="parent"
  33. app:layout_constraintTop_toBottomOf="@+id/tV_descWater"
  34. app:srcCompat="@drawable/ic_water_bottle" />
  35. <TextView
  36. android:id="@+id/tV_descWater"
  37. style="@style/TextStyle"
  38. android:text="@string/product_water"
  39. app:layout_constraintEnd_toEndOf="parent"
  40. app:layout_constraintStart_toStartOf="parent"
  41. app:layout_constraintTop_toTopOf="parent" />
  42. <TextView
  43. android:id="@+id/tV_counterWater"
  44. style="@style/TextStyle"
  45. app:layout_constraintBottom_toBottomOf="parent"
  46. app:layout_constraintEnd_toEndOf="parent"
  47. app:layout_constraintStart_toStartOf="parent" />
  48. </androidx.constraintlayout.widget.ConstraintLayout>
  49. </androidx.cardview.widget.CardView>
  50. </androidx.gridlayout.widget.GridLayout>
  51. </FrameLayout>

另外,这是ProductCardStyle:

  1. <style name="ProductCardStyle">
  2. <item name="cardBackgroundColor">@color/colorPrimary</item>
  3. <item name="android:layout_width">0dp</item>
  4. <item name="android:layout_height">0dp</item>
  5. <item name="android:layout_marginLeft">2dp</item>
  6. <item name="android:layout_marginRight">2dp</item>
  7. <item name="android:layout_marginBottom">2dp</item>
  8. <item name="android:layout_marginTop">2dp</item>
  9. <item name="cardCornerRadius">4dp</item>
  10. <item name="cardElevation">8dp</item>
  11. <item name="layout_columnWeight">1</item>
  12. <item name="layout_rowWeight">1</item>
  13. </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:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;FrameLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. tools:context=&quot;.fragments.DrinksFragment&quot;&gt;
  8. &lt;androidx.gridlayout.widget.GridLayout
  9. android:id=&quot;@+id/mainGrid&quot;
  10. android:layout_width=&quot;match_parent&quot;
  11. android:layout_height=&quot;match_parent&quot;
  12. android:padding=&quot;14dp&quot;
  13. app:alignmentMode=&quot;alignMargins&quot;
  14. app:columnCount=&quot;3&quot;
  15. app:columnOrderPreserved=&quot;false&quot;
  16. app:rowCount=&quot;3&quot;&gt;
  17. &lt;androidx.cardview.widget.CardView
  18. android:id=&quot;@+id/cV_water&quot;
  19. style=&quot;@style/ProductCardStyle&quot;&gt;
  20. &lt;androidx.constraintlayout.widget.ConstraintLayout
  21. android:layout_width=&quot;match_parent&quot;
  22. android:layout_height=&quot;match_parent&quot;
  23. android:layout_gravity=&quot;center_horizontal|center_vertical&quot;
  24. android:layout_margin=&quot;16dp&quot;
  25. android:orientation=&quot;vertical&quot;&gt;
  26. &lt;ImageView
  27. android:id=&quot;@+id/iV_water&quot;
  28. style=&quot;@style/ImageStyle&quot;
  29. android:contentDescription=&quot;@string/ic_water&quot;
  30. app:layout_constraintBottom_toTopOf=&quot;@+id/tV_counterWater&quot;
  31. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  32. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  33. app:layout_constraintTop_toBottomOf=&quot;@+id/tV_descWater&quot;
  34. app:srcCompat=&quot;@drawable/ic_water_bottle&quot; /&gt;
  35. &lt;TextView
  36. android:id=&quot;@+id/tV_descWater&quot;
  37. style=&quot;@style/TextStyle&quot;
  38. android:text=&quot;@string/product_water&quot;
  39. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  40. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  41. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  42. &lt;TextView
  43. android:id=&quot;@+id/tV_counterWater&quot;
  44. style=&quot;@style/TextStyle&quot;
  45. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  46. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  47. app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;
  48. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  49. &lt;/androidx.cardview.widget.CardView&gt;
  50. &lt;/androidx.gridlayout.widget.GridLayout&gt;
  51. &lt;/FrameLayout&gt;

Also, here's the ProductCardStyle:

  1. &lt;style name = &quot;ProductCardStyle&quot;&gt;
  2. &lt;item name = &quot;cardBackgroundColor&quot;&gt;@color/colorPrimary&lt;/item&gt;
  3. &lt;item name = &quot;android:layout_width&quot;&gt;0dp&lt;/item&gt;
  4. &lt;item name = &quot;android:layout_height&quot;&gt;0dp&lt;/item&gt;
  5. &lt;item name = &quot;android:layout_marginLeft&quot;&gt;2dp&lt;/item&gt;
  6. &lt;item name = &quot;android:layout_marginRight&quot;&gt;2dp&lt;/item&gt;
  7. &lt;item name = &quot;android:layout_marginBottom&quot;&gt;2dp&lt;/item&gt;
  8. &lt;item name = &quot;android:layout_marginTop&quot;&gt;2dp&lt;/item&gt;
  9. &lt;item name = &quot;cardCornerRadius&quot;&gt;4dp&lt;/item&gt;
  10. &lt;item name = &quot;cardElevation&quot;&gt;8dp&lt;/item&gt;
  11. &lt;item name = &quot;layout_columnWeight&quot;&gt;1&lt;/item&gt;
  12. &lt;item name = &quot;layout_rowWeight&quot;&gt;1&lt;/item&gt;
  13. &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:

确定