【发布时间】:2020-07-04 20:46:12
【问题描述】:
我在我的项目中使用折叠工具栏布局,其中我使用一个NestedScrollView,其中我有多个卡片视图,其中内容不是静态的,而是在onActivityResult 中更改。我已将CollapsingToolbarLayout 的最小高度设置为 100dp。所以我观察到了其中的几个问题:
- 在大手机上,内容滚动到最小高度 100dp,在底部留下一个大的空白空间,这是不希望的结果。它应该只在必要时向上滚动。必要条件是当我在文本字段中获得大文本值时,它应该滚动,否则不会。
- 我在其中使用了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