Android 折叠NestedScrollView中的工具栏布局和空白

Android 折叠NestedScrollView中的工具栏布局和空白,android,android-layout,android-collapsingtoolbarlayout,android-nestedscrollview,Android,Android Layout,Android Collapsingtoolbarlayout,Android Nestedscrollview,我的折叠工具栏布局有问题。我不想将NestedScrollView滚动到最后,因为如果内容(在我的例子中是文本)很短,它会留下很多空白。以下是截图: 以及: 以下是XML: <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/

我的
折叠工具栏布局有问题
。我不想将
NestedScrollView
滚动到最后,因为如果内容(在我的例子中是文本)很短,它会留下很多空白。以下是截图:

以及:

以下是XML:

<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:id="@+id/coordinator"
   >

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_description"
        >



        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll"
            >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax">

             <android.support.v4.view.ViewPager
                    android:id="@+id/image_pager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_above="@+id/dots_holder" />
                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="15dp"
                    android:id="@+id/dots_holder"
                    android:paddingTop="2dp"
                    android:paddingBottom="2dp"
                    android:gravity="center"
                    android:layout_alignParentBottom="true"
                    android:visibility="gone" />

            </RelativeLayout>


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



    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:padding="8dp"
        android:id="@+id/nested_scrollView">

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TEXT"
                android:id="@+id/name"
                android:textSize="@dimen/abc_text_size_headline_material"
                android:textColor="@color/abc_primary_text_material_light" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TEXT"
                android:id="@+id/othernames"
                android:layout_below="@+id/name"
                android:textSize="@dimen/abc_text_size_subhead_material"
                android:textColor="@color/secondary_text_default_material_light" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="first"
                android:id="@+id/season_head"
                android:layout_alignParentBottom="false"
                android:layout_alignParentLeft="false"
                android:layout_below="@+id/othernames"
                android:layout_marginTop="12dp"
                android:textSize="@dimen/abc_text_size_subhead_material"
                android:textColor="@color/abc_primary_text_material_light" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="second"
                android:id="@+id/places_head"
                android:layout_marginTop="12dp"
                android:textSize="@dimen/abc_text_size_subhead_material"
                android:textColor="@color/abc_primary_text_material_light"
                android:layout_below="@+id/season" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TEXT"
                android:id="@+id/season"
                android:layout_below="@+id/season_head"
                android:layout_marginTop="@dimen/desc_clauses_margin"
                android:textSize="@dimen/abc_text_size_body_1_material"
                android:textIsSelectable="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TEXT"
                android:id="@+id/places"
                android:layout_below="@+id/places_head"
                android:textSize="@dimen/abc_text_size_body_1_material"
                android:layout_marginTop="@dimen/desc_clauses_margin" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="third"
                android:id="@+id/description_head"
                android:textSize="@dimen/abc_text_size_subhead_material"
                android:textColor="@color/abc_primary_text_material_light"
                android:layout_below="@+id/places"
                android:layout_marginTop="12dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TEXT"
                android:id="@+id/description"
                android:layout_below="@+id/description_head"
                android:textSize="@dimen/abc_text_size_body_1_material"
                android:layout_marginTop="@dimen/desc_clauses_margin" />
    </android.support.v4.widget.NestedScrollView>

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


我想在内容结束时停止滚动。

回答这个问题有点晚,但我认为其他开发人员也有类似的问题,因此现在就开始回答了。要理解为什么会发生这种情况,我们必须理解以下核心概念:

  • CoordinatorLayout
    实际上是一个
    FrameLayout
    ,通过一些自定义更改来提供我们看到的行为。因此,当我们将
    AppBarLayout
    NestedScrollView
    作为子级使用时,它们基本上是相互重叠的。CoordinatorLayout获取AppBarLayout的高度,并确保NestedScroll视图显示在AppBarLayout的下方(如果是普通FrameLayout,则根据顺序显示在AppBar的下方或上方)。这就是为什么我们看到的屏幕像上面的截图1

  • 但为什么是空白呢?向上滚动时,Coordinator Layout向上滚动AppBarLayout并折叠
    折叠工具栏
    ,但同时向上移动
    嵌套滚动视图
    ,以提供滚动行为。当
    NestedScrollView
    到达顶部时,现在屏幕具有显示内容的整个视图。由于我们的
    NestedScrollView
    具有
    wrap\u内容
    ,因此它实际上只占用显示上下文所需的空间(TextView),其余区域是应用程序/活动默认背景。我们的活动和
    NestedScrollView
    背景是相同的,因此它给人一种感觉,您的
    NestedScrollView
    占用了整个空间,但实际上没有。尝试将不同的颜色背景设置为
    NestedScrollView
    ,这样会更有意义

  • 如果您真的想实现上述行为,一种方法是避免
    NestedScrollView
    ,并将
    RelativeLayout
    拉出,使其成为外部坐标布局的子对象,它将显示预期的结果。这样做的副作用是,您无法在
    TextView
    内容上获得滚动效果,但工具栏可以工作。如果你想滚动行为,你必须使用NeestDistScVIEW视图,即使没有<代码>协调者< /C>,它也会表现相同,因为没有太多的东西覆盖整个屏幕,应该看到空白。希望这有帮助


    • 回答这个问题有点晚,但我认为其他开发者也有类似的问题,因此现在就开始回答了。要理解为什么会发生这种情况,我们必须理解以下核心概念:

      • CoordinatorLayout
        实际上是一个
        FrameLayout
        ,通过一些自定义更改来提供我们看到的行为。因此,当我们将
        AppBarLayout
        NestedScrollView
        作为子级使用时,它们基本上是相互重叠的。CoordinatorLayout获取AppBarLayout的高度,并确保NestedScroll视图显示在AppBarLayout的下方(如果是普通FrameLayout,则根据顺序显示在AppBar的下方或上方)。这就是为什么我们看到的屏幕像上面的截图1

      • 但为什么是空白呢?向上滚动时,Coordinator Layout向上滚动AppBarLayout并折叠
        折叠工具栏
        ,但同时向上移动
        嵌套滚动视图
        ,以提供滚动行为。当
        NestedScrollView
        到达顶部时,现在屏幕具有显示内容的整个视图。由于我们的
        NestedScrollView
        具有
        wrap\u内容
        ,因此它实际上只占用显示上下文所需的空间(TextView),其余区域是应用程序/活动默认背景。我们的活动和
        NestedScrollView
        背景是相同的,因此它给人一种感觉,您的
        NestedScrollView
        占用了整个空间,但实际上没有。尝试将不同的颜色背景设置为
        NestedScrollView
        ,这样会更有意义

      • 如果您真的想实现上述行为,一种方法是避免
        NestedScrollView
        ,并将
        RelativeLayout
        拉出,使其成为外部坐标布局的子对象,它将显示预期的结果。这样做的副作用是,您无法在
        TextView
        内容上获得滚动效果,但工具栏可以工作。如果你想滚动行为,你必须使用NeestDistScVIEW视图,即使没有<代码>协调者< /C>,它也会表现相同,因为没有太多的东西覆盖整个屏幕,应该看到空白。希望这有帮助


      问题是,当您使用
      AppBarLayout
      appbar\u scrolling\u view\u behavior
      时,
      AppBarLayout
      下的视图(在您的案例中是
      NestedScrollView
      )的大小就像是应用程序条已经从视图中滚动出来一样,然后定位在应用程序条下,切断底部。所以在最初的滚动中,你可能会认为有东西在滚动,但是整个滚动视图只是向上移动,而应用程序栏却在移动。因此,实现结果需要一些复杂的代码来测量内容并根据其大小禁用滚动行为。很抱歉。很遗憾,我无法想出一个折叠工具栏布局的解决方案,但我找到了一个简单的视差效果解决方案,就像在折叠工具栏中一样,解决方案如下:问题是当您使用
      AppBarLayout