2015-06-21 86 views
10

我有以下布局:抽屉,主内容视图具有AppBarLayout,RecyclerView和TextView。当我滚动回收站时,工具栏被正确隐藏。以编程方式显示滚动隐藏后的工具栏(Android设计库)

但是,我有一个用例:当回收站中的所有项目都被移除时,我将它的可见性设置为“不见了”,并显示一个带有相应消息的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> 

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

+0

您是否找到了解决方案?我有同样的问题.. –

+0

@GennadiiSaprykin - 不,我没有,对不起。我认为最终我实施了一些不同的方式,但这已经很长时间了,我不知道它是什么,并从那时起改变了项目。 – wujek

回答

0

U既可以做toolbar.transitionY(int y);在滚动方法或使用可视性消失,你必须在列表视图或回收站视图中添加一个标题与工具栏的大小。因此,整个列表仍然显示

+0

工具栏上没有transitionY方法。你是不是指'setTranslationY'?无论如何,这是行不通的。 – wujek

+0

这里我使用listview而不是recyclerview,但是你可以用它做一个例子。所以我做了什么,我创建了一个自定义列表视图,获取滚动的DeltaY。 (我相信RecyclerView.OnScrollListener已经提供了它,我不确定)然后我翻译工具栏。 [链接](https://github.com/Jjastiny/ToolbarTranslatingListView) –

0

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

如果你想强行显示Toolbar,其实AppBarLayout以抵消顶部和AppBarLayout相关视图底部(它被称为Behavoir)。您需要继续参考AppBarLayout的高度,因为我们知道高度是视图Recttop and bottom之间的距离。

假设你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(); 
    } 
+0

这是行不通的。它不会编译,为此,必须使用“mToolbar.offsetTopAndBottom”。但是,没有任何反应。 – wujek

7

您可以通过访问包含您的工具栏

这里AppBarLayout做到这一点是一个例子:

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

setExpanded (扩展,动画)将完成这项工作。 您也可以对AppBarLayout进行引用,而不是从工具栏中调用getParent。

+0

它也适用于TabLayout而不是ToolBar! – Annihil

相关问题