2016-07-25 79 views

回答

0

這是一種簡單的。 只是把這個代碼工具欄

<android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:layout_scrollFlags="scroll|enterAlways"/> 

在這樣做的魔力:app:layout_scrollFlags="scroll|enterAlways"

記得在你的XML添加應用程序調用xmlns:app="http://schemas.android.com/apk/res-auto"

檢查本教程:https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout

1

由於您的活動有其內容視圖中的工具欄開始的片段,你可以隨時從片段得到它保持。

MainActivity mainActivity = (MainActivity)getActivity(); 

我建議做它的方法在您的MainActivity:

public void showToolbar(boolean show) { 

    // If you have your toolbar as a private member of MainActivity 
    toolbar.setVisiblity(show ? View.VISIBLE : View.GONE); 

    // But you can also do this 
    if (show) { 
     getSupportActionBar().show(); 
    } 
    else { 
     getSupportActionBar().hide(); 
    } 
} 

然後當你真正想從你的片段隱藏它,叫它:

((MainActivity)getActivity()).showToolbar(false); 

要使用戶界面變得更加流暢,我推薦翻譯它,而不是立即隱藏它。看看上面的答案在這裏尋找靈感:

android lollipop toolbar: how to hide/show the toolbar while scrolling?

如果你不知道如何照顧時,實際上顯示或通過滾動邏輯隱藏它,看看這個庫,手柄很多關於你和也給出了例子:

https://github.com/ksoichiro/Android-ObservableScrollView

0

使用下面的代碼:

<android.support.design.widget.CoordinatorLayout 
xmlns:app="http://schemas.android.com/apk/res-auto" 
    ...> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_scrollFlags="scroll|enterAlways"/> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v7.widget.RecyclerView 
      android:id="@+id/rvToDoList" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

</android.support.design.widget.CoordinatorLayout> 

如果回收站View是另一片段,然後在下面的行添加到含有RecyclerView在此CoordinatorLayout視圖。

app:layout_behavior="@string/appbar_scrolling_view_behavior" 

必須使用CoordinatorLayout