[英]How to animate both top and bottom Toolbars(or any other view) enter/exit screen on scroll in CoordinatorLayout?
[英]Hide top and bottom toolbars when using CoordinatorLayout
我設法弄清楚了如何向CoordinatorLayout
添加底部的工具欄,但似乎無法弄清楚如何隱藏底部的RelativeLayout
。
如果我將@string/appbar_scrolling_view_behavior
添加到底部的RelativeLayout
,則當用戶向上滾動時,底部的條會出現。 理想的效果是當用戶向上滾動時, 頂部和底部的條都會出現。 有什么想法我將如何處理嗎? 因為我的ViewPager
包含來自其他庫的復雜代碼,所以需要跨所有選項卡的通用底部欄(用於簡單操作)。
浮動動作按鈕不是可取的,因為它會將動作集合隱藏在需要用戶點擊並展開的按鈕內。 以下是我用於管理標簽頁和尋呼機的xml布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:ads="http://schemas.android.com/tools">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#1378BB"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<android.support.v7.widget.Toolbar
android:id="@+id/tabs"
android:background="#3202c4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
android:layout_alignParentBottom="true"
android:layout_marginBottom="100dp"
/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
有什么想法或想法嗎? 我嘗試將工具欄放置在CollapsingToolbarLayout內,並使用layout_alignParentBottom="true"
,但這僅將工具欄錨定到AppBarLayout
而不是整個CoordinatorLayout
僅當我可以讓@string/appbar_scrolling_view_behavior
觸發底部的RelativeLayout
的另一種方式時(使條形圖以相反的方向滾動時才顯示)
您需要將自定義CoordinatorLayout.Behavior
添加到底部的Toolbar
。 Toolbar
必須是CoordinatorLayout
的直接子代。
標記為:
app:layout_behavior="{name_of_the_class_of_behavior}"
app:layout_scrollFlags="scroll|enterAlways"
在自定義行為中,覆蓋方法:
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View fab, View dependency) {
return dependency instanceof AppBarLayout;
}
第二個是:
public boolean onDependentViewChanged(CoordinatorLayout parent, View fab, View dependency)
您可以在其中控制目標視圖的可見性。 基本上,需要測量顯示的AppBarLayout
哪一部分, AppBarLayout
進行轉換並將其相應地設置為您的視圖:
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) view.getLayoutParams();
int viewBottomMargin = lp.bottomMargin;
int distanceToScroll = view.getHeight() + viewBottomMargin;
float ratio = dependency.getY() / toolbarHeight;
view.setTranslationY(-distanceToScroll * ratio);
在這里了解更多
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.