[英]Recyclerview items not showing with the same height
我正在將RecyclerView
與GridLayoutManager
。 您在圖像中看到的我的問題是,其中一項具有較長的文本時,網格項的高度不同。 我正在從firebase加載數據,並在列表包含數據時通知Adapter
。 我嘗試了所有方法,但找不到能自動適應項目的解決方案,如所附的第二張圖片。 有什么幫助嗎?
這是我的recyclerview項目布局
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="rtl"
>
<android.support.v7.widget.CardView
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:cardCornerRadius="10dp"
android:layout_margin="5dp"
app:cardBackgroundColor="#fff"
app:cardElevation="5dp"
>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/cartoonImg"
android:layout_width="match_parent"
android:layout_height="120dp"
android:scaleType="fitXY"
app:imgUrl="@{cartoon.thumb}"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
tool:text="Cartoon Name"
android:gravity="center"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:textColor="@color/colorPrimary"
android:text="@{cartoon.title}" />
</LinearLayout>
</android.support.v7.widget.CardView>
</FrameLayout>
這將完全滿足您的需求
嘗試用以下方法替換您的
TextView
:
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
tool:text="Cartoon Name"
android:gravity="center"
android:minLines="2"
android:maxLines="2"
android:ellipsize="end"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:textColor="@color/colorPrimary"
android:text="@{cartoon.title}" />
您可以為textview設置一個靜態高度,這樣所有人的視角都相同。 但是您可能會因此而丟失文字。
您可以添加到TextView中:
android:ellipsize="start"
android:maxLines="1
通過API 26,您可以使用Autosizing TextView-在此處查看文檔: https : //developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview
您正在使用線路
android:layout_height="wrap_content"
在CardView
,由於后代具有match_parent
,因此TextView
的大小會更改。
為了解決這個問題,您可以在TextView
使用以下屬性android:maxLines="1"
。 這將阻止TextView跳至下一行,但會裁剪文本。
或者,您可以使用靜態高度而不是match_parent
或wrap_content
,即使用一個值。 我強烈建議使用48dp
因為這是《 材料設計指南》的建議。 資源:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.