2017-02-25 66 views
1

我有一個ListView只顯示一個項目。如果我將ListView設置爲一個高度,我可以看到所有項目都被加載到它中,但wrap_content僅顯示第一行。Android ListView只顯示第一項

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

<com.fmsirvent.ParallaxEverywhere.PEWImageView 
    android:id="@+id/logo" 
    android:layout_width="match_parent" 
    android:layout_height="370dp" 
    android:layout_gravity="center" 
    android:contentDescription="@string/show_logo" 
    android:scaleType="centerCrop" /> 

<GridView 
    android:id="@+id/hostGrid" 
    android:layout_width="match_parent" 
    android:layout_height="240dp" 
    android:layout_gravity="center" 
    android:columnWidth="100dp" 
    android:numColumns="auto_fit" /> 

<TextView 
    android:id="@+id/showDescription" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="16dp" 
    android:textSize="20sp" /> 

<ListView 
    android:id="@+id/showListView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
</LinearLayout> 

更新:截圖

The listView only displays the first item and cannot be scrolled

+1

對於ListView的高度,不要使用'wrap_content'。你可以在那裏做'match_parent',它會佔用'LinearLayout'中剩下的空間,因爲這是最後一件事。 –

回答

2

通常你想一個ListView,RecyclerView等爲佔用所有的剩餘空間,也許有一個最小高度。既然你已經在LinearLayout中有你的,這將很好地工作:

<ListView 
    android:id="@+id/showListView" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" /> 

如果你發現它是在某些設備上太小,你需要把的LinearLayout成某種滾動型的,並設定最低高度ListView。

要做到這一點,您需要使用NestedScrollView。達到此目的的最佳方式將如下所示:

<android.support.v4.widget.NestedScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

     <com.fmsirvent.ParallaxEverywhere.PEWImageView 
      android:id="@+id/logo" 
      android:layout_width="match_parent" 
      android:layout_height="370dp" 
      android:layout_gravity="center" 
      android:contentDescription="@string/show_logo" 
      android:scaleType="centerCrop" /> 

     <GridView 
      android:id="@+id/hostGrid" 
      android:layout_width="match_parent" 
      android:layout_height="240dp" 
      android:layout_gravity="center" 
      android:columnWidth="100dp" 
      android:numColumns="auto_fit" /> 

     <TextView 
      android:id="@+id/showDescription" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="16dp" 
      android:textSize="20sp" /> 

     <ListView 
      android:id="@+id/showListView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

    </LinearLayout> 

</android.support.v4.widget.NestedScrollView> 

希望這會有所幫助。

+0

謝謝,我試過你建議的代碼,但它沒有工作。我以爲你不應該把一個ListView放在一個ScrollView中 – robbiestells

+0

看看[NestedScrollView](https://developer.android.com/reference/android/support/v4/widget/NestedScrollView.html)這確切的目的。我用這裏的一個例子來編輯我的答案。 – DanielLaneDC

+0

感謝您的嘗試,仍然無法爲我工作 – robbiestells