2016-07-25 30 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

相关问题