簡體   English   中英

在 AppBarLayout 中滾動第二個孩子

[英]Scroll second child in AppBarLayout

我試圖獲得這種效果,如果用戶滾動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 一起滾動。

我想要實現的是將第一個孩子(工具欄)固定在適當的位置,並讓第二個孩子(一個LinearLayout )滾動並隱藏在工具欄后面。 不幸的是,我無法理解這種行為。

是否可以不使用第 3 部分庫? 在此先感謝並為我的英語感到抱歉。

最后,我想出了一種實現這種行為的方法,通過將 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>

希望有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM