【问题标题】:Collapsing Toolbar Layout Scroll even when there is no scrolling content in Nested Scroll View即使嵌套滚动视图中没有滚动内容,也会折叠工具栏布局滚动
【发布时间】:2020-07-04 20:46:12
【问题描述】:

我在我的项目中使用折叠工具栏布局,其中我使用一个NestedScrollView,其中我有多个卡片视图,其中内容不是静态的,而是在onActivityResult 中更改。我已将CollapsingToolbarLayout 的最小高度设置为 100dp。所以我观察到了其中的几个问题:

  1. 在大手机上,内容滚动到最小高度 100dp,在底部留下一个大的空白空间,这是不希望的结果。它应该只在必要时向上滚动。必要条件是当我在文本字段中获得大文本值时,它应该滚动,否则不会。
  2. 我在其中使用了Natario Solution,它动态计算CollapsingToolbarLayout 的最小高度,这会以某种方式工作,但它会消除NestedScrollView 的滚动能力,如果我得到大的动态文本,这是不希望的结果。

我的 layout.xml 文件是:-

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:clickable="true"
    android:id="@+id/topBlock"
    app:layout_constraintTop_toTopOf="parent"
    android:focusable="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/navigation"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbar"
            app:elevation="0dp"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:background="@null"
            android:minHeight="100dp">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collapse_bar"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="@android:color/holo_blue_light"

                android:minHeight="100dp"

                app:layout_collapseMode="parallax"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
                app:layout_scrollInterpolator="@android:anim/decelerate_interpolator">


            </com.google.android.material.appbar.CollapsingToolbarLayout>

        </com.google.android.material.appbar.AppBarLayout>

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/camera"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:elevation="8dp"
            app:backgroundTint="@color/white"
            app:fabSize="normal"
            app:layout_anchor="@+id/nestedScrollView"
            app:layout_anchorGravity="top|center_horizontal"
            app:srcCompat="@drawable/ic_launcher_background" />

        <androidx.core.widget.NestedScrollView
            android:id="@+id/nestedScrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white"
            app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent">

       

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:background="@color/white"
                    android:layout_height="wrap_content">

                    ////My Scrolling Content(Contains many edit texts so its size may vary)
                </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.core.widget.NestedScrollView>


    </androidx.coordinatorlayout.widget.CoordinatorLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

我没有在 MyActivity.kt 文件中做任何事情。请帮我解决这个问题。从过去 15 天开始,我已经尝试了这个社区中几乎所有的解决方案,但仍然没有运气。谢谢!!

编辑:正如建议的那样,我已将问题更新为 NestedScrollView 作为直接子级,并发布了 Android Studio 预览的屏幕截图,其中它正在跨越预览的高度。它应该根据内容取高度。如果我制作NestedScrollView wrap_content 那么它仍然在底部留下空白空间。在后台我有相机运行,所以我必须让它匹配父级以使覆盖全高。

【问题讨论】:

  • 我可以看到一个明显的事情是您的嵌套滚动视图不是您的协调器布局的直接子级,因此对折叠工具栏没有影响。此外,您的视图层次结构是错误的,并且存在许多其他可能导致该问题的问题。
  • 我无法想象这将如何在任何安卓设备上顺利运行。您应该考虑学习基本的编程原则,例如关注点分离:en.wikipedia.org/wiki/Separation_of_concerns。以及 Android 特定原则,例如一个 Activity 多个 Fragments 方法:developer.android.com/guide/components/fragments#Design。
  • @MohammadOmidvar 是的,当然,谢谢,我将添加 nestedScrollView 作为 CoordinatorLayout 的直接子级并检查。
  • @RenéSpies 是的,谢谢,我会查看链接,但我已经有单活动应用程序。我使用 SOC、RXJAVA 数据绑定、MVVM 架构、DI 一切来达到 Android 设计标准。我只是在我的 xml 中遇到问题,所以我修改了我的 xml 以使其在没有任何资源依赖或 Fragment 的情况下易于运行。

标签: android android-layout android-coordinatorlayout android-collapsingtoolbarlayout


【解决方案1】:

我对您的问题没有特别的解决方案,但我强烈建议您采用 Motion Layout。我也在开发一个带有“折叠工具栏”的应用程序,我想做一个视差效果,就像在 Spotify 中一样。使用 Coordinator Layout 和 Collapsing Toolbar 真的很困难,除了关于它的文档不是很广泛,老实说我不知道​​我在做什么。

我在网上研究了其他方法,最后我发现了这一点。 Motion Layout 可让您调整动画到非常小的细节。我知道考虑将整个布局从 CoordinatorLayout 更改为 MotionLayout 似乎需要做很多工作,但事实并非如此。如果有什么好处的话,因为 Motion Layout 从 Constraint Layout 扩展而来,并且您可以为布局的每个视图设置动画(如果您的父级是 Motion Layout)。另一个支持 ML 的论点是您有动画侦听器:例如,当转换完成时,会自动调用一个方法,这样您就可以在需要时链接动画,这真的很有趣。

  1. 查看一些您可以使用 Motion Layout 构建的可能的动画。这些带有布局 xml 和动画 xml,因此您可以从中学习(顺便说一句,很棒的官方文档): https://developer.android.com/training/constraint-layout/motionlayout/examples

  2. 这是我用来模仿 Coordinator Layout 中的 Collapsing Toolbar 行为的教程: https://blog.stylingandroid.com/motionlayout-collapsing-toolbar-part-1/ https://blog.stylingandroid.com/motionlayout-collapsing-toolbar-part-2/ (我个人只是经历了第 1 部分,然后我自己继续)

【讨论】:

  • 感谢@Agustin Magne。我还删除了折叠工具栏布局,因为我没有特定要求。我最终得到了简单的嵌套滚动视图,顶部有透明背景和一些逻辑,并得到了预期的效果。这种运动布局对我来说是新的,我一定会尝试的。
  • 这并不能直接回答问题。这更像是另一个主题的另一种方法。
  • 真的!我刚刚添加了这个,因为 OP 正在寻找一个“最新”的答案:)
【解决方案2】:

首先,不需要ConstraintLayout,因为你有CoordinatorLayout。正如 cmets 中的用户所提到的,NestedScrollView 应该是 CoordinatorLayout 的直接子代,而不是 ConstraintLayout。以这种方式设计,某些行为可能无法按预期工作,实际上是错误的设计。

我已经进行了更改,这是您在预览中看到的内容:

更新布局:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/topBlock"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapse_bar"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:background="@android:color/holo_blue_light"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:layout_scrollInterpolator="@android:anim/decelerate_interpolator">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/myToolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme"
                app:title="Your title" />

        </com.google.android.material.appbar.CollapsingToolbarLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:id="@+id/nestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="40dp"
            android:layout_marginEnd="20dp"
            android:layout_marginRight="20dp">

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dp"
                android:text="MyRandomText"
                android:textColor="@android:color/black"
                android:textSize="42sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toTopOf="@+id/view"
                app:layout_constraintCircleRadius="0dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <View
                android:id="@+id/view"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="20sp"
                android:background="@android:color/holo_blue_light"
                app:layout_constraintTop_toBottomOf="@id/title" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="8dp"
        android:src="@mipmap/ic_launcher"
        app:backgroundTint="@color/white"
        app:fabSize="normal"
        app:layout_anchor="@+id/nestedScrollView"
        app:layout_anchorGravity="top|center_horizontal" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

P.S:还请记住,NestedScrollView 应该只有一个孩子,因为它是第一个孩子。

这样是正确的做法,您不必使用 kotlin-java 代码来实现滚动或其他行为,您将能够使用标志和属性来实现这些行为,例如 layout_scrollFlags 和exitUntilCollapsed 之类的。

【讨论】:

  • 感谢您的回答,但仍有空白空间。我已经用屏幕截图更新了问题以使其清楚。
  • @AfzalKhan 尝试将android:clipToPadding="false" 和android:layout_height="wrap_content" 添加到您的NestedScrollView。我会更新答案
  • 我使用了它,但它仍在滚动到 100dp 的最小高度。只有在嵌套滚动视图中有足够的内容时,它才应该滚动,这是小型手机的情况。在大手机上,如果所有内容都可见,则不应滚动。
  • 这可能是因为CollapsingToolbarLayout中的android:minHeight="100dp"我居然忘记删除了。请尝试更新后的答案。
  • 您的代码使工具栏始终固定,无论内容是否足以滚动
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-03
  • 2015-08-14
  • 1970-01-01
相关资源
最近更新 更多