3

我正在開發一個Android應用程序。我不擅長Android開發。在我的應用程序中,我使用RecyclerView和CardView與StaggeredGridLayoutManager。因爲我希望列表中的每個單元格大小彼此不同,並像下圖中那樣交錯。在Android中使用RecyclerView和StaggeredGridLayoutManager時單元格不交錯

enter image description here

但是當我運行我的應用程序,細胞不交錯,它們在尺寸上彼此相等。

這是截圖

enter image description here

這是我回收視圖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沒有交錯?我該如何解決它?

回答

1

你的電池項目都得到了相同的大小,因爲沒有什麼會改變大小...我想你想獲得更大的項目,如果你有更大的圖片

首先這個變化:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

這樣:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

然後代替:

<ImageView 
     android:id="@+id/di_iv_image" 
     android:scaleType="centerCrop" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

嘗試:

<ImageView 
     android:id="@+id/di_iv_image" 
     android:scaleType="center" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

和縮放圖像的高度,以你希望的大小將他們面前;) 與cropCenter您始終獲得圖像裁剪相同大小

+0

如果將圖像scaleType設置爲居中,則圖像寬度將與單元格寬度不匹配。路線不正確。特別是與文本。 –

+0

是的,這是正確的,但你必須縮放至少圖像高度,然後將其添加到imageView。你能告訴我你添加圖片的代碼部分嗎? – 666

+0

沒什麼特別的。我只是使用畢加索直接設置圖像。沒有修改。 –

1

設定imageview with android:adjustViewBounds="true"

+0

這不能提供問題的答案。一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你將能夠[評論任何職位](http://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/評論/低質量帖/ 14080377) –

+0

感謝您的澄清@MoralesBatovski – Chenmin

-1

你需要強制調整高度以達到你想要的效果。

我使用的數據綁定,從而,一組高度是這樣的:

public int getHeight(int position) { 
    Random r = new Random(); 
    int randomHeight = r.nextInt(Math.abs(mRecyclerViewHeight)/9) + Math.abs(mRecyclerViewHeight)/4; 
    if(position%2 == 0){ 
     randomHeight += Math.abs(mRecyclerViewHeight)/9; 
    } 
    return randomHeight; 
} 

@Override 
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 


     ((DiscoveryViewHolder) holder).bindRepository(mPostList.get(position), getHeight(position)); 

持有者).bindRepository(mPostList.get(位置)); }

+0

什麼是mRecyclerViewHeight價值或如何得到它? – blackjack

+0

對不起,遲到的答案。 mRecyclerViewHeight是佈局中的回收視圖組件的高度。 –