2016-03-02 113 views
0

我目前使用RecyclerView來顯示卡片列表。 它與ListViewArrayListAdapter很好,但現在發生了一些奇怪的事情。LinearLayoutManager創建卡片之間的空間

當列表第一次加載時,顯示的3張第一張卡片顯示正確。但是沒有出現的那些在它們每一個下面都有巨大的空間。

它與LayoutManager有關,因爲Android文檔說它是管理顯示與否以及如何顯示它的實體。

就我而言,我使用的是LinearLayoutManager

這裏是在我的初始化的一些代碼我Fragment

layoutManager = new LinearLayoutManager(getActivity()); 
layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
listAdapter = new EventsListAdapter(events); 
recyclerView.setAdapter(listAdapter); 
recyclerView.setLayoutManager(layoutManager); 

這裏的RecyclerView佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/events_list_recycler_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" /> 
</RelativeLayout> 

最後我CardView聲明

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/event_row_cardview"> 
    ... 

回答

0

我發現的來源問題。

我的CardView被包裹在一個LinearLayout的屬性android:layout_height="match_parent"中,所以每次渲染時都會佔用屏幕的整個高度。

我修改了它的屬性爲android:layout_height="wrap_content"