2015-06-21 85 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

相關問題