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

12
我有以下布局:一个抽屉,主内容视图包含AppBarLayout、RecyclerView和TextView。当我滚动RecyclerView时,工具栏会正确隐藏。 但是,我有一个使用情况:当从RecyclerView中删除所有项目时,我将其可见性设置为“gone”,并显示一个带有适当消息的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>

顺便说一下,由于某种原因,如果我像所有教程中所示,将AppBarLayout放在RecyclerView之后,它会隐藏列表中最顶部的项目。只有当它在RecyclerView之前时,它才能正常工作。


你找到解决方法了吗?我也遇到了同样的问题。 - Gennadii Saprykin
@GennadiiSaprykin - 不好意思,我没有这样做。我想最终我实现了不同的东西,但是已经过了很长时间,我不知道那是什么,并且从那时起改变了项目。 - wujek
3个回答

11
你可以通过访问包含工具栏的AppBarLayout来完成它。
这是一个例子:
if (mToolbar.getParent() instanceof AppBarLayout){
  ((AppBarLayout)mToolbar.getParent()).setExpanded(true,true);
}

调用setExpanded(expand,animation)函数即可实现此功能。

您还可以通过引用AppBarLayout而无需从工具栏中调用getParent方法。


它也可以与TabLayout一起使用,而不是ToolBar! - anni

1

您需要将RecyclerView的头部放置在AppBarLayout的高度上。也就是说,在RecyclerView的位置0处添加标题,然后添加其余元素。

如果您想强制显示Toolbar,实际上是通过偏移AppBarLayout依赖视图的顶部和底部来完成的(这被称为Behavoir)。您需要保留AppBarLayout的高度的引用,因为我们知道高度是视图Rect的顶部和底部之间的距离。

假设您的AppBarLayout只包含一个Toolbar

int mAppBarLayoutHeight = mAppBarLayout.getBottom() - mAppBarLayout.getTop(); //initial, normal height

 private void showToolbar(){
        if(this.mAnimator == null) {
            this.mAnimator = new ValueAnimator();
            this.mAnimator.setInterpolator(new DecelerateInterpolator());
            this.mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                 int animatedOffset = (int) animation.getAnimatedValue();
                 mToolbar.offsetTopBottom(animatedOffset/2);
                }
            });
        } else {
            this.mAnimator.cancel();
        }

        this.mAnimator.setIntValues(0, mAppBarLayoutHeight);
        this.mAnimator.start();
    }

这个不起作用。它也无法编译 - 必须使用 mToolbar.offsetTopAndBottom。但是,仍然没有任何反应。 - wujek

0

你可以在滚动方法中使用toolbar.transitionY(int y); 或者使用visibility gone,并在列表视图或回收视图中添加一个具有工具栏大小的标题,以便整个列表仍然显示。


Toolbar上没有transitionY方法。你是不是想说setTranslationY?无论如何,它都不起作用。 - wujek
这里我使用的是ListView而不是RecyclerView,但你可以根据它创建一个示例。所以我创建了一个自定义的ListView来获取滚动的DeltaY。(我相信RecyclerView.OnScrollListener已经提供了它,但我不确定)然后我翻译了工具栏。链接 - Justin Yang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接