【问题标题】:Toolbar will not collapse with Scrollview as child of CoordinatorLayoutScrollview 作为 CoordinatorLayout 的子项时,工具栏不会折叠
【发布时间】:2015-08-19 04:35:36
【问题描述】:

我正在尝试按照 Google Docs 使用 CoordinatorLayout,但我遇到了 CoordinatorLayout 内的 ScrollView 的问题。基本上,向下滚动时,工具栏通常会与 RecyclerView 或 Listview 一起折叠。现在有了 ScrollView,它就不会崩溃。

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >

        <TextView
            android:id="@+id/tv_View"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/filler"
            style="@style/TextAppearance.AppCompat.Large"
            />

    </ScrollView>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            />

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

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

【问题讨论】:

  • CoordinatorLayout 中使用NestedScrollView 代替ScrollView,也适用于BottomNavigationView
  • 或者你可以ScrollViewandroid:nestedScrollingEnabled="true"

标签: android toolbar android-scrollview android-toolbar coordinator-layout


【解决方案1】:

ScrollView 不与CoordinatorLayout 合作。你必须使用NestedScrollView 而不是ScrollView

【讨论】:

  • 这确实回答了这个问题,工具栏不会折叠,因为CoordinatorLayout 不支持ScrollView,OP 可以通过从ScrollView 切换到NestedScrollView 来获得请求的行为。
  • @dbugger 为什么这个答案没有提供答案?
  • 我花了一个多小时试图弄清楚为什么它不能正常滚动。谢谢!
  • 确认工作 - 即使在通过 supportFragmentManager 事务替换的嵌套片段中
  • 非常感谢! NestedScrollView 在视图分页器内时也可以与 CoordinatorLayout 一起正常工作
【解决方案2】:

使用 NestedScrollView 将您的滚动视图折叠为 Coordinator Layout 的子级。 用以下代码替换您的代码:

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

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

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

        <TextView
            android:id="@+id/tv_View"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/filler"
            style="@style/TextAppearance.AppCompat.Large"
            />

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

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            />

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

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

【讨论】:

    【解决方案3】:

    您可以保留 ScrollView 并添加此 XML 属性:android:nestedScrollingEnabled="true" 以便它知道 CoordinatorLayout 作为同级,请记住此属性仅在 lollipop 版本中受支持 并且以上

    【讨论】:

      【解决方案4】:

      使用CoordinatorLayout 时,请使用NestedScrollView 而不是常规的ScrollView

      要使CollapsingToolbarLayout 滚动,您可以触发滚动 行为通过设置子布局的最小高度 NestedScrollView 到 *1000dp。

      android:minHeight="1000dp"
      

      布局:

      <android.support.v4.widget.NestedScrollView
      app:layout_behavior="@string/appbar_scrolling_view_behavior">
      
          <!--to trigger scroll behavior-->
          <LinearLayout android:minHeight="1000dp"/>
      
      </android.support.v4.widget.NestedScrollView>
      

      *SupportDesignDemos 示例在这里:https://github.com/android/platform_development/blob/master/samples/SupportDesignDemos/res/layout/include_appbar_scrollview.xml

      【讨论】:

      • NestedScrollView 上设置android:layout_height="match_parent" 就足够了
      【解决方案5】:

      实际的答案应该是CoordinatorLayout不适用于ScrollView,因为ScrollView没有实现NestedScrollingChild接口。 NestedScrollView 是带有 NestedScrollingChild 实现的 ScrollView。如果你想了解更多关于嵌套滚动的信息,我发了一个blog post 来讨论它。

      【讨论】:

      • 请贴出博文中的相关代码以及它如何解决OP的问题。
      • 我只是想指出问题的根本原因。接受的解决方案没问题,但没有解释为什么用 NestedScrollView 更改 ScrollView 可以解决问题。
      • 我只是说,您的解决方案可能很好,它只需要您的部分代码,想象您的博客被删除(无论出于何种原因)然后,它可能变得无用。我不太喜欢 Android 开发,但我或多或少知道 Stack Overflow 是如何工作的……不是 100% 必要的,但如果你在答案中添加代码(这个和未来的),你的问答会好得多。这只是一个建议,如果你跟进,我会很高兴支持
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      • 1970-01-01
      • 1970-01-01
      • 2015-09-13
      相关资源
      最近更新 更多