【问题标题】:Hiding AppBarLayout on RecyclerView Scroll using MaterialDesignComponents使用 MaterialDesignComponents 在 RecyclerView Scroll 上隐藏 AppBarLayout
【发布时间】:2019-07-30 20:40:06
【问题描述】:

当我向上滚动 RecyclerView 时,我试图折叠 AppBarLayout。我正在尝试使用 Material Design Components。

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <com.google.android.material.appbar.MaterialToolbar
            android:layout_height="?attr/actionBarSize"
            android:layout_width="match_parent"
            android:id="@+id/toolbar"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
            <com.google.android.material.textview.MaterialTextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center"
                android:textSize="18sp"
                android:text="@string/app_name"/>
        </com.google.android.material.appbar.MaterialToolbar>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/component_recycler_view"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="24dp"
        android:layout_gravity="bottom|end"
        android:contentDescription="@string/extended_fab_content_desc"
        android:text="@string/extended_fab_label"
        app:icon="@drawable/ic_plus_24dp"
        app:layout_anchor="@id/component_recycler_view"
        app:layout_anchorGravity="bottom|right|end"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="24dp"
        android:layout_marginBottom="24dp"
        app:layout_constraintBottom_toBottomOf="@id/component_recycler_view"/>

</androidx.constraintlayout.widget.ConstraintLayout>

当我在 recyclerview 上向上滚动时,它确实向上滚动,但 appbarlayout 并没有隐藏自己。此外,RecyclerView 的最顶层项目部分隐藏在 AppBarLayout 后面。

【问题讨论】:

    标签: android android-recyclerview android-toolbar android-appbarlayout material-components-android


    【解决方案1】:

    您可以使用CoordinatorLayout 来实现CollapsingToolbarLayout,如下所示:

    <androidx.coordinatorlayout.widget.CoordinatorLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/appbar"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleGravity="bottom"
            app:expandedTitleMarginEnd="16dp"
            app:expandedTitleMarginStart="16dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:title="@string/choose_hero">
    
            <ImageView
                android:id="@+id/toolbar_image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:contentDescription="@null"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop" />
    
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat" />
        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    我正在使用这种类型的布局,我的应用栏正在折叠/展开,而没有隐藏任何 recyclerView 项目。

    【讨论】:

    • 如果我不想要 ImageView 怎么办?还能用吗?
    • 我看不出有什么理由不这样做,请尝试使用没有 imageview/您想要的其他视图的代码并告诉我它是否有效。
    • 嗯,就是这样。我只想要其中写有应用程序名称的工具栏。在它下面是recyclerview。当 recyclerview 向上滚动时,appbarlayout 也应该向上滚动
    • 机器人使用任何视图怎么样?你可以试试这个吗?
    • 嘿,谢谢你的回答。对您的代码进行一些修改,我已经得到了我需要的东西:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2020-11-16
    • 2015-09-29
    • 1970-01-01
    相关资源
    最近更新 更多