【问题标题】:Collapsing toolbar and nestedscrollview not scrolling smoothly折叠工具栏和nestedscrollview滚动不顺畅
【发布时间】:2015-12-14 08:47:50
【问题描述】:

嵌套滚动视图在向下滚动时平滑滚动,但在向上滚动时缓慢。向上滚动时折叠工具栏(带有图像视图和框架布局)不会呈现其内容(保持空白)。我已经尝试了折叠工具栏中的每个标志。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">


<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"

        app:contentScrim="?attr/colorPrimary"

        app:layout_scrollFlags="scroll|exitUntilCollapsed">


        <ImageView
            android:id="@+id/backdrop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="100dp"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            android:src="@drawable/pic"
            app:layout_collapseMode="parallax"
            app:layout_collapseParallaxMultiplier="0.5"
            app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"/>

        <include
            android:id="@+id/framelayout"
            layout="@layout/header_layout"
            app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
            android:minHeight="100dp"/>


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


<!-- Your Scrollable View -->
<android.support.v4.widget.NestedScrollView
    android:id="@+id/nested"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="fill_vertical"

    app:layout_behavior="@string/appbar_scrolling_view_behavior">

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



      </LinearLayout>



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


<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="#da1b75"
    android:orientation="horizontal"
    android:textColor="#ffffff"
    android:theme="@style/ThemeOverlay.AppCompat.Light"
    app:layout_anchor="@id/appbar"
    app:layout_collapseMode="pin"
    app:title="">



</android.support.v7.widget.Toolbar>

【问题讨论】:

  • 我也遇到了同样的问题
  • 工具栏不应该在折叠工具栏布局内吗?我猜应该只有一个视差视图,而其他视图应该是折叠工具栏布局中的工具栏
  • 你的 NestedScrollView 中是否有复杂的 Views?没有 CoordinatorLayout 你的 NestedScrollView 滚动流畅吗?

标签: android android-support-library android-appbarlayout nestedscrollview android-collapsingtoolbarlayout


【解决方案1】:

我想你忘记了 CoordinatorLayout 的结束标签。另外,将工具栏放在 CollapsingToolbarLayout 上

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"

            app:contentScrim="?attr/colorPrimary"

            app:layout_scrollFlags="scroll|exitUntilCollapsed">


            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="100dp"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/pic"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.5"
                app:layout_scrollFlags="scroll|exitUntilCollapsed" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="#da1b75"
                android:orientation="horizontal"
                android:textColor="#ffffff"
                android:theme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_anchor="@id/appbar"
                app:layout_collapseMode="pin"
                app:title="Your title">

            </android.support.v7.widget.Toolbar>


<!-- What is this for?

            <include
                android:id="@+id/framelayout"
                layout="@layout/header_layout"
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
                android:minHeight="100dp"/>
-->

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


    <!-- Your Scrollable View -->
    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nested"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"

        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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



          </LinearLayout>



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

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

【讨论】:

    【解决方案2】:

    我在包含coordinator layout 的布局和包含RecyclerView 的片段中遇到了类似的问题。我在CollapsingToolbarLayout 中添加了以下代码以使滚动顺畅。

    app:layout_scrollFlags="scroll|enterAlways"
    

    【讨论】:

      【解决方案3】:

      使嵌套滚动视图的直接子元素可点击(如果您没有一个子元素,请添加一个布局并将所有 UI 元素放入其中并使布局可点击)
      android:clickable= “真”
      我遇到了类似的问题,这对我有用!

      这是我一直在编写的代码 -

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="test.testrecycler.MaterialActivity"
      >
      
      <android.support.design.widget.AppBarLayout
          android:layout_width="match_parent"
          android:layout_height="300dp"
          android:theme="@style/AppTheme.AppBarOverlay"
          android:fitsSystemWindows="true"
          >
          <android.support.design.widget.CollapsingToolbarLayout
              android:id="@+id/collapsing_toolbar"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:contentScrim="?attr/colorPrimary"
              app:layout_scrollFlags="scroll|exitUntilCollapsed"
              app:expandedTitleTextAppearance="@style/expandedappbar"
              app:collapsedTitleTextAppearance="@style/collapsedappbar"
              app:statusBarScrim="@color/colorPrimaryDark"
              >
              <ImageView
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:src="@drawable/sample"
                  android:scaleType="centerCrop"
                  app:layout_collapseMode="parallax" />
              <android.support.v7.widget.Toolbar
                  android:id="@+id/toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="?attr/actionBarSize"
                  app:layout_collapseMode="pin" />
      
          </android.support.design.widget.CollapsingToolbarLayout>
      </android.support.design.widget.AppBarLayout>
      
      <include layout="@layout/content_material" />
      
      </android.support.design.widget.CoordinatorLayout>
      

      这是具有 NestedScrollView 的文件 content_material.xml。它有一个可点击的子元素(TextView)。

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/content_material"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      tools:context="test.testrecycler.MaterialActivity"
      tools:showIn="@layout/activity_material">
      
      <TextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:clickable="true"
          android:text="This is an association football club formed in 1983, based in the town of Dover, Kent, England. In the 1989–90 season Dover
          Athletic won the Southern League championship, but failed to gain promotion to the Football Conference as the club's ground did not meet
          the required standard. Three seasons later the team won the title again and this time gained promotion to the Conference, where they spent
          nine seasons before being relegated. The club was transferred to the Isthmian League Premier Division in 2004, but another poor season led
          the club to a further relegation. After three seasons in the Isthmian League Division One South, the club won the championship and promotion
          back to the Premier Division, and the following season won another championship and promotion to Conference South. In the 2013–14 season,
          Dover defeated Ebbsfleet United to return to the Conference Premier after a twelve-year absence. Nicknamed the Whites for their white shirts,
          they have played at the Crabble Athletic Ground since the club's formation. Their best performance in the FA Cup was reaching the third
          round proper in both the 2010–11 and 2014–15 seasons. (Full article...)
          Recently featured: Kalki Koechlin Rogue River (Oregon) Operation Ironside
          Archive By email More featured articles...
          his time gained promotion to the Conference, where they spent
          nine seasons before being relegated. The club was transferred to the Isthmian League Premier Division in 2004, but another poor season led
          the club to a further relegation. After three seasons in the Isthmian League Division One South, the club won the championship and promotion
          back to the Premier Division, and the following season won another championship and promotion to Conference South. In the 2013–14 season,
          Dover defeated Ebbsfleet United to return to the Conference Premier after a twelve-year absence. Nicknamed the Whites for their white shirts,
          they have played at the Crabble Athletic Ground since the club's formation. Their best performance in the FA Cup was reaching the third
          round proper in both the 2010–11 and 2014–15 seasons. (Full article...)" />
      </android.support.v4.widget.NestedScrollView>
      

      【讨论】:

        【解决方案4】:

        更新:您只需在 ANDROID STUDIO 中右键单击图像并将图像转换为 webp 即可减小图像大小。这将大大减小图像大小

        它非常简单。我挣扎了很多,终于知道这是我放在ImageView的src中的高分辨率图像的问题。 解决方案: 只需降低图像的分辨率,保持尺寸不变。 (可能使用 Photoshop)。

        为我工作

        【讨论】:

          【解决方案5】:

          CoordinatorLayout 和 CollapsingToolbarLayout 平滑滚动是一个错误,Google 仍然没有修复它。 :|

          移除 NestedScrollView。带有 app:layout_behavior="@string/appbar_scrolling_view_behavior" 的 RecyclerView 就够了,修复一下。

          您可以使用第三方库:https://github.com/henrytao-me/smooth-app-bar-layout

          【讨论】:

            【解决方案6】:

            如果您的清单文件有 android:hardwareAccelerated="false" 行,请将其删除。

            【讨论】:

              【解决方案7】:

              经过几次尝试,我找到了正确的解决方案,不是一次正确,但它会解决您的问题,

              只需尝试将 CollapsingToolbarLayout layout_scrollInterpolator 从 decelerate_interpolator 更改为 liniare,例如:linear_interpolator。

              <com.google.android.material.appbar.CollapsingToolbarLayout
                          android:id="@+id/collapsingtoolbar"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          app:expandedTitleMargin="0dp"
                          app:layout_scrollFlags="scroll|exitUntilCollapsed"
                          app:layout_scrollInterpolator="@android:anim/linear_interpolator">
              

              希望对你有帮助。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2016-04-04
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2019-05-25
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多