首页 > 解决方案 > 向下滚动时无法隐藏默认操作栏

问题描述

向下滚动回收站视图时如何隐藏默认操作栏?我需要在我的 XML 中创建一个新的操作栏吗?我希望它在用户向下滚动时消失,然后在用户再次向上滚动时重新出现。这在安卓应用程序中很常见。目前我的 XML 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerViewObjectives"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fabNewObjective"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="16dp"
    android:background="@color/colorPrimaryLight"
    android:clickable="true"
    android:focusable="true"
    app:srcCompat="@drawable/ic_new_objective"
/>

</RelativeLayout>

标签: androidkotlin

解决方案


你必须使用协调器布局来做你想做的事。您还需要在布局中为 appbar 创建一个布局,以便协调器布局可以控制它。这里解释得更好: https ://android-developers.googleblog.com/2015/05/android-design-support-library.html 注意:如果你有一个浮动操作按钮,你也应该使用协调器布局。如果你不这样做,那么它就不能动态响应键盘显示或快餐栏等事件。


推荐阅读