编程秀滚动工具栏隐藏后(Android的设计库) [英] Programmatically show Toolbar after hidden by scrolling (Android Design Library)

查看:270
本文介绍了编程秀滚动工具栏隐藏后(Android的设计库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下布局:一个抽屉,用具有AppBarLayout,RecyclerView和一个TextView的主要内容视图。当我滚动回收,工具栏隐藏正确

I have the following layout: a drawer, with the main content view having a AppBarLayout, RecyclerView and a TextView. When I scroll the recycler, the toolbar is correctly hidden.

不过,我有一个用例:当从回收站中的所有项目都去掉了,我将它的知名度'走了',并用它来代替显示相应的消息一个TextView。如果在工具栏隐藏,这样做,这是不可能的用户再次看到工具栏。

However, I have a use case: when all items from the recycler are removed, I sets its visibility to 'gone' and a TextView with an appropriate message it shown instead. If this is done while the toolbar is hidden, it is not possible for the user to see the toolbar again.

是否有可能以编程方式使工具栏尽显?我会做这个,只要显示,而不是RecyclerView TextView的。

Is it possible to programmatically cause the toolbar to be fully shown? I would do this whenever the TextView is shown instead of the 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>

BTW,出于某种原因,如果我把RecyclerView后AppBarLayour,因为所有教程似乎表明,它从列表中隐藏了最上面的项目。它只是正常工作时,它是在它上面。

BTW, for some reason, if I put the AppBarLayour after the RecyclerView, as all tutorials seem to show it, it hides the topmost item from the list. It only works correctly when it is above it.

推荐答案

您需要把标题为 RecyclerView 来的高度 AppBarLayout 。即在RecyclerView 0位置,你需要添加标题,然后元素的其余部分。

You need to put header to the RecyclerView to the height of the AppBarLayout. I.e at position 0 of the RecyclerView you need to add the header and then the rest of the elements.

如果你想强行显示工具栏,其实 AppBarLayout 以抵销顶部和底部的 AppBarLayout 相关视图(它被称为 Behavoir )。你需要保持高度的参考 AppBarLayout ,因为我们知道,高度的顶部和底部的观点 矩形

If you want to forcefully show the Toolbar, actually the AppBarLayout with offsetting top and bottom of the AppBarLayout dependent views (it is called Behavoir) . You need to keep reference of height of the AppBarLayout, as we know that height is the distance between top and bottom of view Rect.

假设你的 AppBarLayout 仅持有一个工具栏

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();
    }

这篇关于编程秀滚动工具栏隐藏后(Android的设计库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆