【发布时间】:2020-06-05 03:17:48
【问题描述】:
所以在我创建的 Recyclerview 中,我使用 CardView 设置项目布局。这是项目布局:
product_list_item_layout.xml
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/CardView"
android:id="@+id/sampleProductCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
app:cardCornerRadius="12dp"
app:cardElevation="6dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="8dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher_background"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Apple Pie"
android:textColor="#6f6f6f"
android:textSize="14sp"
android:id="@+id/productName"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Rp. "
android:textColor="#6f6f6f"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="2dp"
android:text="Price"
android:textColor="#6f6f6f"
android:textSize="12sp"
android:id="@+id/productPrice"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="Stock :"
android:textColor="#000"
android:textSize="10sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="6dp"
android:text="4"
android:textColor="#000"
android:textSize="10sp"
android:id="@+id/productStockQuantity"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
在 Android Studio 中,设计预览显示为 this。
这是recyclerview布局:
fragment_product_list.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".mainFragments.ProductListFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
...
...
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvProductList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="16dp"
/>
...
</LinearLayout>
</layout>
在片段中,我将其设置为 GridLayout。但它显示为this。
关于细节,product_list_item.xml 和 android:id="@+id/productName" 中的 textview 的长度有很多变化,因为我从 API 端点获取数据。
我认为片段或 recyclerview 适配器没有任何问题。
我在这里错过了什么吗?如果有任何我遗漏的细节,请在评论中提及。
【问题讨论】:
-
能不能把ImageView的高度改成wrap_content
-
我明白了。多谢,伙计。现在Textview问题解决了。
-
不客气,兄弟。我把它作为答案,将来可能会帮助其他人
标签: android android-recyclerview