当使用CollapsingToolbarLayout时,NestedScrollView无法滚动到底部。

19

我想使用带有CollapsingToolbarLayout的NestedScrollView。在NestedScrollView中有非常长的内容。不幸的是,我无法滚动到底部。一些长内容被截断了。奇怪的是,当我旋转屏幕时,滚动正常工作,所有内容都可见。

<android.support.design.widget.CoordinatorLayout
    android:fitsSystemWindows="true"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <android.support.design.widget.AppBarLayout
        android:fitsSystemWindows="true"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

        <android.support.design.widget.CollapsingToolbarLayout
            android:fitsSystemWindows="true"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:fitsSystemWindows="true"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:scaleType="centerCrop"
                android:src="@drawable/u8"
                app:layout_collapseMode="parallax"/>

            <android.support.v7.widget.Toolbar
                android:layout_height="?attr/actionBarSize"
                android:layout_width="match_parent"
                app:layout_collapseMode="pin"/>

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:clipToPadding="false"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

            <!-- lots of widgets-->

        </LinearLayout>

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

</android.support.design.widget.CoordinatorLayout>

编辑:我注意到剪切内容的高度与工具栏的高度相同。


如果将LinearLayout的高度设置为“wrap_content”会发生什么?通常,在滚动小部件内部的任何布局都会在滚动方向上获得“wrap_content”。 - kris larson
仍然无法滚动到底部。 - Aleksander Mielczarek
同样的问题在这里。我尝试在不同的地方添加 android:fitsSystemWindows="true",并且在清单文件中添加了 android:windowSoftInputMode="adjustResize"。到目前为止所有的想法都失败了。 - jkj yuio
嘿,伙计!你找到解决方案了吗? - Wijay Sharma
老实说,我现在不记得了。 - Aleksander Mielczarek
显示剩余2条评论
7个回答

11

以下翻译源自此处。在NestedScrollView中添加paddingBottom属性可以解决这个问题:

android:paddingBottom="<toolbar height in collapsed state>"

1
如果您使用默认的工具栏高度,可以使用?attr/actionBarSize - Peter Fortuin

8

我也遇到了类似的问题,即当键盘打开时,NestedScrollView无法滚动到底部。

将AppBarLayout放在NestedScrollView之后对我很有帮助。如果这个方法对您有效,请告诉我。


6
我曾经遇到过同样的问题。这个错误的原因之一是没有设置 SupportActionBar。
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

(我没有这样做是因为我只需要折叠工具栏正常工作所需的工具栏,而且我认为setSupportActionBar不重要)

另一个问题是:它在活动中工作得很好,但在片段中却无法正常工作(也许是我使用的特定版本的支持库的问题)


4
我的问题是CollapsingToolbarLayout中的 "exitUntilCollapsed" 行: app:layout_scrollFlags="scroll|exitUntilCollapsed"> 当和“scroll”一起使用时,会出现问题。我也不能使用 marginBottom,因为我有一个即时翻译选项,它会使用新内容刷新 TextViews,当这种情况发生时,它会神秘地决定滚动得更远,这最终导致底部出现一个非常难看的空白空间。
我使用了 "enterAlwaysCollapsed" 代替它,并将我的工具栏移到顶部,在折叠外面。虽然不完全是我想要的,但到目前为止,我找不到解决办法。

1
非常感谢,我花了一个星期的时间研究它。对我有用。 - Rohit Sharma

3

距离提出这个问题已经有一段时间了。但是可能像这个答案中所示,设置CollapsingToolbarLayout中的minHeight属性也可以帮助某些人。


这个答案是正确的,我可以提供一些“开发者式的解释”来支持这个解决方案,但不确定是否200%正确。 - BobTheCat

2

由于工具栏被固定(带有CollapsingToolbar),NestedScrollView未能调整底部视图与屏幕的位置。

如果你将工具栏设置在setSupportActionBar上,NestedScrollView将会适应屏幕。

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); 

0
使用 app:layout_behavior="@string/appbar_scrolling_view_behavior" 属性和嵌套滚动视图即可实现。
<android.support.design.widget.CoordinatorLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <android.support.design.widget.AppBarLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:layout_scrollFlags="scroll|enterAlways"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

        </android.support.design.widget.AppBarLayout>
        <android.support.v4.widget.NestedScrollView
                android:id="@+id/task_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fillViewport="true"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
        </android.support.v4.widget.NestedScrollView>
    </android.support.design.widget.CoordinatorLayout>

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