NestedScrollView无法与高度为match_parent的子控件一起滚动。

41

我使用NonSwipeableViewPager实现了一个具有NestedScrollView的fragment,期望scrollview可以向上滚动并显示两个文本视图:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <include
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                layout="@layout/header" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="16dp"
                android:src="@drawable/ic_up" />

        </RelativeLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text 1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text 2" />

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

但是它无法滚动,我尝试了很多方法,但仍然没有得到任何解决方案


2
ScrollView需要子元素的高度为wrap_content。 - Tim
@TimCastelijns,我们没有任何方法可以使用match_parent高度来实现它,是吗? - Norutan
1
这就是ScrollView的工作原理,你可以将ScrollView设置为与父级匹配,但不能将ScrollView的子元素设置为match parent,需要使用wrap content。 - Tim
在子元素中,您可以设置类似以下的内容: android:layout_height="wrap_content" android:minHeight="50dp" - avisper
6个回答

133
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

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

这个LinearLayout应该有android:layout_height="wrap_content"的属性。

原因是如果ScrollView的子元素与ScrollView本身的大小相同(高度均为match_parent),那么就没有可滚动的内容,因为它们的大小相同,而ScrollView的高度只会与屏幕一样高。

如果LinearLayout的高度是wrap_content,则其高度与屏幕高度无关,ScrollView将能够滚动其中的内容。

请记住,一个ScrollView只能有1个直接的子元素,并且该子元素需要使用android:layout_height="wrap_content"属性。


16
对我来说,只有 fillViewPort = true 才起作用。在我的情况下,子元素的高度并不重要。 - Dhruvam Gupta
是的,我也一样!只需使用 fillViewPort = true 就可以了。但是 @Tim 说到的 android:layout_height 有时确实是正确的,在过去遇到过问题。 - sud007
请注意,fillViewPort=true会使滚动视图占据屏幕上剩余的空间,如果滚动视图本身的内容不足以填充整个视图。 - Tim

15
在我这种情况下,app:layout_behavior="@string/appbar_scrolling_view_behavior" 只有在某些人遇到问题并尝试后才能解决问题。你还应该添加 android:fillViewport="true",但没有这个也可以正常工作。
 <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/subscription_background"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

12
我认为 app:layout_behavior 属性只有在使用 CoordinatorLayout 布局时才适用。 - Sumit Shukla

5

当我在androidx.core.widget.NestedScrollView中的最后一个子元素中添加了"android:layout_marginBottom="100dp""时,对我有用。


不是解决方案,但我看到了同样的问题,我还不理解为什么滚动视图不知道内容是可滚动的。 - Boy
我也遇到了同样的问题,我添加了 "android:layout_marginBottom="200dp" 这一行代码后问题得以解决。 - Toby

1
在嵌套滚动视图中使用约束布局,并将底部约束设置为父级,将顶部约束设置为上面的 UI 组件,然后将左右约束设置为 0。内容将正确滚动。我尝试过这个方法,希望你也能成功。
<androidx.core.widget.NestedScrollView
    android:id="@+id/nestedScrollView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/materialToolbar"
    tools:ignore="ExtraText">

而子元素的约束应该是“匹配父级”:

 <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

最终的用户界面如下所示:

 <androidx.core.widget.NestedScrollView
    android:id="@+id/ScrollView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/materialToolbar"
    tools:ignore="ExtraText">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.cardview.widget.CardView
            android:id="@+id/cardView"
            style="?attr/materialCardViewFilledStyle"
            android:layout_width="380dp"
            android:layout_height="220dp"
            app:cardCornerRadius="20dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:fontFamily="@font/inter_medium"
                    android:text="Best Seller of the week"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toTopOf="@+id/textView"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="@+id/imageView3" />

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="159dp"
                    android:layout_height="62dp"
                    android:fontFamily="@font/inter_semibold"
                    android:gravity="left|center"
                    android:text="MJ's Hazelnut Flavour Chocolate"
                    android:textAlignment="inherit"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toBottomOf="@+id/imageView3"
                    app:layout_constraintStart_toStartOf="@+id/textView3"
                    app:layout_constraintTop_toTopOf="@+id/imageView3" />

                <com.google.android.material.button.MaterialButton
                    android:id="@+id/btnMoreInfo"
                    style="@style/Widget.Material3.Button.TextButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:fontFamily="@font/inter_bold"
                    android:text="More Info"
                    app:icon="@drawable/arrow"
                    app:iconGravity="end"
                    app:layout_constraintBottom_toBottomOf="@+id/imageView3"
                    app:layout_constraintStart_toStartOf="@+id/textView"
                    app:layout_constraintTop_toBottomOf="@+id/textView" />

                <ImageView
                    android:id="@+id/imageView3"
                    android:layout_width="144dp"
                    android:layout_height="188dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginBottom="8dp"
                    android:scaleType="centerCrop"
                    android:src="@drawable/choco2"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintCircleRadius="20dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.cardview.widget.CardView>


        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:fontFamily="@font/inter_medium"
            android:text="This week's recommendations"
            android:textSize="18dp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@+id/btnSeeAll"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/btnSeeAll" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btnSeeAll"
            style="@style/Widget.Material3.Button.TextButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:fontFamily="@font/inter_semibold"
            android:text="See all"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/cardView" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvHome"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:nestedScrollingEnabled="false"
            android:orientation="horizontal"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btnSeeAll"
            tools:listitem="@layout/rv_item_home" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvPromo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:orientation="horizontal"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/rvHome"
            tools:listitem="@layout/rv_promo" />

        <TextView
            android:id="@+id/textView7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:fontFamily="@font/inter_medium"
            android:text="A few words from us"
            android:textSize="18dp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@+id/btnSeeAll2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/btnSeeAll2"
            app:layout_constraintVertical_bias="0.25" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btnSeeAll2"
            style="@style/Widget.Material3.Button.TextButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:fontFamily="@font/inter_semibold"
            android:text="See all"
            android:textAppearance="@style/TextAppearance.Material3.LabelLarge"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="@+id/rvPromo"
            app:layout_constraintTop_toBottomOf="@+id/rvPromo" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvAboutUs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="16dp"
            android:orientation="horizontal"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btnSeeAll2"
            tools:listitem="@layout/rv_about_us" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

组件树以供参考: 点击此处了解......愉快编码


0
如果您使用了如下的netedscrollview,那么您需要使用android:scrollbars="vertical"。
<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@id/toolbar_updateUserDetails"
    app:layout_constraintBottom_toBottomOf="parent"
    android:fillViewport="true">
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:scrollbars="vertical"
        >
</LinearLayout>
</androidx.core.widget.NestedScrollView>

-3

你需要计算你的其他子元素,因为最后一个子元素绑定在了nestedScrollView上。你可以像子元素高度一样添加margin。这样就能正常工作了。


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