通过滚动隐藏后以编程方式显示工具栏(Android设计库)

wuj*_*jek 10 android android-design-library

我有以下布局:抽屉,主内容视图有AppBarLayout,RecyclerView和TextView.滚动回收器时,工具栏被正确隐藏.

但是,我有一个用例:当删除回收器中的所有项目时,我将其可见性设置为"已消失",并将TextView设置为显示相应的消息.如果在隐藏工具栏时完成此操作,则用户无法再次看到工具栏.

是否可以以编程方式使工具栏完全显示?每当显示TextView而不是RecyclerView时,我都会这样做.

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    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:animateLayoutChanges="true">

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

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

            <Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?android:attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways"/>

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/test_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

        <TextView
            android:id="@+id/empty_test_list_info_label"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:padding="@dimen/spacing_big"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:visibility="gone"/>

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

    <RelativeLayout
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="?android:attr/windowBackground"
        android:elevation="10dp"/>

</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

顺便说一句,出于某种原因,如果我将AppBarLayour放在RecyclerView之后,就像所有教程似乎都显示它一样,它会隐藏列表中最顶层的项目.它只在它上面时才能正常工作.

小智 9

您可以通过访问包含工具栏的AppBarLayout来完成此操作

这是一个例子:

if (mToolbar.getParent() instanceof AppBarLayout){
  ((AppBarLayout)mToolbar.getParent()).setExpanded(true,true);
}
Run Code Online (Sandbox Code Playgroud)

setExpanded(expand,animation)将完成这项工作.您还可以引用AppBarLayout,而不是从工具栏中调用getParent.


小智 0

你可以做toolbar.transitionY(int y); 在滚动方法上或使用可见性消失,您必须在列表视图或回收器视图中添加具有工具栏大小的标题。这样整个列表仍然显示