我正在開發一個Android應用程序。我不擅長Android開發。在我的應用程序中,我使用RecyclerView和CardView與StaggeredGridLayoutManager。因爲我希望列表中的每個單元格大小彼此不同,並像下圖中那樣交錯。在Android中使用RecyclerView和StaggeredGridLayoutManager時單元格不交錯
但是當我運行我的應用程序,細胞不交錯,它們在尺寸上彼此相等。
這是截圖
這是我回收視圖XML
<android.support.v7.widget.RecyclerView
android:id="@+id/df_rv_destination"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
這是XML細胞項目
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/di_iv_image"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:textSize="17dp"
android:textColor="@color/white"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:id="@+id/di_tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.CardView>
這是我如何配置我的RecyclerView與StaggerGridLayoutManager
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, 1);
rcDestinations.setLayoutManager(staggeredGridLayoutManager);
rcDestinations.setItemAnimator(new DefaultItemAnimator());
regionsList = new ArrayList<Region>();
destinationsAdapter = new DestinationsAdapter(getActivity(),regionsList);
rcDestinations.setAdapter(destinationsAdapter);
那麼爲什麼我的RecyclerView沒有交錯?我該如何解決它?
如果將圖像scaleType設置爲居中,則圖像寬度將與單元格寬度不匹配。路線不正確。特別是與文本。 –
是的,這是正確的,但你必須縮放至少圖像高度,然後將其添加到imageView。你能告訴我你添加圖片的代碼部分嗎? – 666
沒什麼特別的。我只是使用畢加索直接設置圖像。沒有修改。 –