16

我试图获得这种效果,如果用户滚动RecyclerView某个布局与回收器一起向上滚动并消失在Toolbar.

使用 可以获得类似的行为CoordinatorLayout,这可以通过设置

app:layout_behavior="@string/appbar_scrolling_view_behavior"

在上述回收站上,并做

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

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways"/>

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

另外,如果我将第二个孩子放在AppBarLayout, 并设置app:layout_scrollFlags为它,获得的效果是相同的,两个布局都与 Recycler 一起滚动。

我想要实现的是将第一个孩子(工具栏)固定在适当的位置,并让第二个孩子(a LinearLayout)滚动并隐藏在工具栏后面。不幸的是,我无法理解这种行为。

是否可以不使用第 3 部分库?在此先感谢并为我的英语感到抱歉。

4

1 回答 1

8

最后,我想出了一种实现这种行为的方法,通过将 CoordinatorLayout 包含在 LinearLayout 中并通过将 Toolbar 移动到外部(root)级别使第二个子项(LinearLayout)成为第一个子项

之前的层次结构:

<CoordinatorLayout>
 <AppBarLayout>
  <ToolBar>
  <LinerarLayout>

之后的层次结构:

<LinearLayout>
  <ToolBar>
  <CoordinatorLayout>
   <AppBarLayout>
     <LinearLayout>

一个例子:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        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"
        android:orientation="vertical">
        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="48dp" />
        <androidx.coordinatorlayout.widget.CoordinatorLayout
            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:elevation="16dp">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/colorSecondaryLight"
                    android:orientation="vertical"
                    app:layout_scrollFlags="scroll"/>
                </com.google.android.material.appbar.AppBarLayout>
                .
                .
                .
                .
            </androidx.coordinatorlayout.widget.CoordinatorLayout>
    </LinearLayout>

希望有帮助!

于 2018-11-20T19:36:34.690 回答