【问题标题】:Programmatically show Toolbar after hidden by scrolling (Android Design Library)通过滚动隐藏后以编程方式显示工具栏(Android设计库)
【发布时间】:2015-09-07 06:59:19
【问题描述】:

我有以下布局:一个抽屉,主要内容视图有一个 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>

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

【问题讨论】:

  • 您找到解决方案了吗?我有同样的问题..
  • @GennadiiSaprykin - 不,我没有,抱歉。我想最后我实现了一些不同的东西,但是已经很长时间了,我不知道它是什么,并且从那时起改变了项目。

标签: android android-design-library


【解决方案1】:

您可以通过访问包含您的工具栏的 AppBarLayout 来做到这一点

这是一个例子:

if (mToolbar.getParent() instanceof AppBarLayout){
  ((AppBarLayout)mToolbar.getParent()).setExpanded(true,true);
}

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

【讨论】:

  • 它也适用于 TabLayout 而不是 ToolBar!
【解决方案2】:

您需要将标题放在RecyclerView 的高度到AppBarLayout 的高度。即在 RecyclerView 的位置 0,您需要添加标题,然后添加其余元素。

如果你想强制显示Toolbar,实际上是AppBarLayoutAppBarLayout 依赖视图的顶部和底部偏移(称为Behavoir)。您需要保留AppBarLayout 的高度参考,因为我们知道高度是top and bottom 与视图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。不过,什么也没发生。
【解决方案3】:

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

【讨论】:

  • 工具栏上没有transitionY方法。你的意思是setTranslationY?无论如何,它不起作用。
  • 这里我使用 listview 而不是 recyclerview 但你可以用它做一个例子。所以我所做的是我创建了一个自定义列表视图,它可以获取滚动的 DeltaY。 (我相信 RecyclerView.OnScrollListener 已经提供了我不确定)然后我翻译工具栏。 link
猜你喜欢
  • 1970-01-01
  • 2016-03-01
  • 2015-08-23
  • 1970-01-01
  • 2014-12-19
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
  • 2020-01-17
相关资源
最近更新 更多