有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android ListView仅显示第一项

我有一个只显示一个项目的列表视图。如果我将ListView设置为一个设置的高度,我可以看到所有的项目都被加载到其中,但是wrap_内容只显示第一行

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

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

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

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

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

更新:截图

The listView only displays the first item and cannot be scrolled


共 (1) 个答案

  1. # 1 楼答案

    通常,您希望ListView、RecyclerView等占据所有剩余空间,并且可能具有最小高度。既然你已经把自己的头发放在了一条直线上,这将很好地发挥作用:

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

    希望这有帮助