我有一个固定的工具栏,一个带有 layout_scrollFlags="scroll" 的视图,一个用于过滤的固定视图和一个回收器视图,在 CoordinatorLayout 中按照从上到下的顺序排列。
这是我到目前为止能够实现的:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<RelativeLayout
android:id="@+id/scrollAwayView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
app:layout_scrollFlags="scroll">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Scroll Away View"
android:textColor="@color/black"
android:textSize="20sp" />
</RelativeLayout>
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/fixedToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/apl_grayscale_050"
app:title="Fixed Toolbar"
app:titleTextColor="@color/black" />
<HorizontalScrollView
android:id="@+id/horizontalScroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:clipToPadding="false"
android:gravity="bottom"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:scrollbars="none">
<com.google.android.material.chip.ChipGroup
android:id="@+id/chipGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleLine="true" />
</HorizontalScrollView>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
问题是:定位不正确。固定工具栏应位于顶部,而滚动视图应位于两个固定视图之间:固定工具栏和芯片组列表。
我读到
layout_scrollFlags=scroll
在任何兄弟姐妹观点时无效
在这之前没有这个标志。关于如何在保持职位的同时实现这种行为有什么想法吗?
经过大量的反复试验,我今天找到了解决方案。
解决方案
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/fixedToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#F5F5F5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="Fixed Toolbar"
app:titleTextColor="@color/black" />
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/fixedToolbar">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<RelativeLayout
android:id="@+id/scrollAwayView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:elevation="0dp"
app:layout_scrollFlags="scroll">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Scroll Away View"
android:textColor="@color/black"
android:textSize="20sp" />
</RelativeLayout>
<HorizontalScrollView
android:id="@+id/horizontalScroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:clipToPadding="false"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:scrollbars="none">
<com.google.android.material.chip.ChipGroup
android:id="@+id/chipGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleLine="true" />
</HorizontalScrollView>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
这里的重点:
fixedToolbar
位于父 Coordinator 布局之外的视图中。fixedToolbar
的父级和 CoordinatorLayout 作为视图其余部分的父级。我是如何得出这个解决方案的
检查
layout_scrollFlags
,我看到了以下关于scroll
标志的信息:
视图将滚动与滚动事件直接相关。这面旗帜 需要设置任何其他标志才能生效。如果任何兄弟姐妹 之前的 views 没有这个 flag,那么这个值有 no 效果.
因为我需要在
scrollAwayView
上方放置一个视图,所以我将它放置在 Coordinator 父视图之外的视图中。现在 fixedToolbar
和 scrollAwayView
不是兄弟视图,同时被放置在正确的排列中。