简体   繁体   English

Android ListView仅显示第一项

[英]Android ListView only shows first item

I have a ListView that is only showing one item. 我有一个ListView仅显示一项。 If I make the ListView a set height, I can see that all the items are getting loaded into it, but the wrap_content is only showing the first row. 如果将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>

Update: screenshot 更新:屏幕截图

listView仅显示第一项,无法滚动

Generally you want a ListView, RecyclerView, etc to take up all of the remaining space and perhaps have a minimum height. 通常,您希望ListView,RecyclerView等占据所有剩余空间,并且可能具有最小高度。 Since you've got yours in a LinearLayout, this will work nicely: 由于您已将其放置在LinearLayout中,因此可以很好地工作:

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

If you find it's too small on some devices, you will need to put the LinearLayout into some sort of ScrollView and set a minimum height on the ListView. 如果在某些设备上发现它太小,则需要将LinearLayout放入某种ScrollView中,并在ListView上设置最小高度。

To do that, you will need to use a NestedScrollView . 为此,您将需要使用NestedScrollView The best way to achieve this will be something like this: 实现此目标的最佳方法如下所示:

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

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM