ListView中的match_parent不起作用,如果ListView位于NestedScrollView内部

10

在我的应用程序中,我正在使用ListView并将其放置在NestedScrollView中。当我将ListViewheight设置为match_parent时,它并没有占满整个屏幕。我希望ListView能够覆盖整个屏幕。

我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:isScrollContainer="false"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

        <ListView
            android:id="@+id/list"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:divider="@null" />
        </LinearLayout>
</android.support.v4.widget.NestedScrollView>

1
正如@Reza所提到的,您不应该在另一个可滚动视图(如ScrollView)中使用可滚动视图(如ListView)。那么为什么您希望将ListView放在NestedScrollView中呢? - Rajen Raiyarela
你在其他布局中使用这个NestedScrollView吗?如果有,请发布它。 - SRB Bans
@RajenRaiyarela 我正在使用最新的Material Design库开发应用程序。我希望在滚动ListView时隐藏我的工具栏。因此,NestedScrollView对于我的应用程序来说是必需的。 - user5292016
我将其用作ViewPager的片段。 - user5292016
我是指,使用自定义适配器还是默认的ArrayAdapter? - SRB Bans
显示剩余15条评论
3个回答

43
将以下内容添加到您的NestedScrollView中。
android:fillViewport="true"

3
这应该是一个答案。 - Paresh Dudhat
我尝试了许多hackery方法,直到我找到了这一行解决方案,非常感谢! - usernotnull
完美的一个。谢谢。 - anuradha

3

-1

我不确定这个解决方案是否能帮助你,但我用这个方法解决了和你一样的问题。

 <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minHeight="@dimen/min_height"> //minimum height for listview

请尝试删除ListView上方的LinearLayout或在LinearLayout中添加"android:minHeight="@dimen/min_height""。 - yemonkyaw

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接