【问题标题】:Android ListView only shows first itemAndroid ListView 只显示第一项
【发布时间】:2017-02-25 00:16:12
【问题描述】:

我有一个只显示一项的 ListView。如果我将 ListView 设置为设定的高度,我可以看到所有项目都被加载到其中,但 wrap_content 只显示第一行。

<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>

更新:截图

【问题讨论】:

  • 不要wrap_content 获得ListView 的高度。你可以在那里做match_parent,它只会占用LinearLayout中的其余空间,因为它是最后一件事。

标签: java android listview


【解决方案1】:

通常,您希望 ListView、RecyclerView 等占据所有剩余空间,并且可能具有最小高度。既然你已经在 LinearLayout 中得到你的,这将很好地工作:

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

如果您发现它在某些设备上太小,则需要将 LinearLayout 放入某种 ScrollView 并在 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>

希望这会有所帮助。

【讨论】:

  • 谢谢,我尝试了您建议的代码,但没有成功。我认为您不应该将 ListView 放在 ScrollView 中
  • 查看NestedScrollView,它就是为此目的而制作的。我已经编辑了我的答案,并在此处举例说明了它的用法。
  • 感谢您的尝试,似乎仍然不适合我
  • 你能发一张你看到的截图吗?
  • 在@DanielLaneDC 上方添加
猜你喜欢
  • 2016-09-13
  • 1970-01-01
  • 2017-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多