【发布时间】:2021-01-17 16:46:21
【问题描述】:
我想实现一个带有 Recyclerview 的应用栏,例如 Gmail 和 Play 商店中的应用栏,如下所示:
我尝试在 CoordinatorLayout 内使用 AppBarLayout,但 App Bar 位于 Recyclerview 上方
但是,我希望它像一个叠加层,所以如果它是透明的或有一些边距,recyclerview 内容如下所示:
我尝试使用 recyclerview 将应用栏置于相对布局中,但当我向下滚动时它不会隐藏(即使使用滚动标志也始终固定在顶部)。
这是我的 XML 代码:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.MainFragment"
android:background="@color/lightBackground">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/tab_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/main_action_bar_size"
android:background="@color/white">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_scrollFlags="scroll|enterAlways|snap">
<FrameLayout
android:id="@+id/appBarDrawer"
android:layout_width="@dimen/main_action_bar_size"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_menu" />
</FrameLayout>
<FrameLayout
android:id="@+id/appBarSearch"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/appBarDrawer"
app:layout_constraintRight_toLeftOf="@+id/appBarVoiceSearch"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:maxLines="2"
android:text="@string/search"
android:textColor="@color/lightGrayFont"
android:textSize="15sp" />
</FrameLayout>
<FrameLayout
android:id="@+id/appBarVoiceSearch"
android:layout_width="@dimen/main_action_bar_size"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toLeftOf="@+id/appBarMore"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_mic_none" />
</FrameLayout>
<FrameLayout
android:id="@+id/appBarMore"
android:layout_width="@dimen/main_action_bar_size"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_more" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</RelativeLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:menu="@menu/menu_bottom_navigation"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我还知道当它隐藏或显示时要捕捉什么而不影响像 Play 商店中那样的 recyclerview 滚动(似乎在一个不同的层中)。
【问题讨论】:
标签: android android-layout material-ui material-design android-appbarlayout