NestedScrollView无法使用match_parent height子进行滚动

时间:2016-05-13 09:16:15

标签: android android-nestedscrollview

我使用像这样的NestedScrollView实现NonSwipeableViewPager片段,我期望的是scrollview可以向上滚动并显示2个textviews:

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

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

6 个答案:

答案 0 :(得分:60)

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

此线性布局应具有android:layout_height="wrap_content"

原因是如果scrollview的子节点与scrollview本身的大小相同(高度均为match_parent),则表示没有任何内容可以滚动,因为它们的大小相同,而scrollview将只有屏幕一样高。

如果linearlayout的高度为wrap_content,则高度与屏幕的高度无关,并且滚动视图将能够滚动它。

请记住,一个scrollview只能有一个直接的孩子,而那个孩子需要android:layout_height="wrap_content"

答案 1 :(得分:3)

在我的情况下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">

答案 2 :(得分:1)

对我来说,当我为androidx.core.widget.NestedScrollView中的最后一个孩子添加“ android:layout_marginBottom =“ 100dp”“时,它起作用了

答案 3 :(得分:0)

如果您按照以下方式使用了 netedscrollview,则必须使用 android:scrollbars="垂直"

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

答案 4 :(得分:-1)

您必须计算另一个孩子,因为上一个孩子绑定在nestedScrollView上。您添加边距,例如孩子的身高。它正在工作。

答案 5 :(得分:-9)

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