2017-02-25 135 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