From d4fe719a2cc45c0cbc8117d92293be1898287d2a Mon Sep 17 00:00:00 2001 From: Benjamin Gamard Date: Fri, 19 Oct 2018 13:34:45 +0200 Subject: [PATCH] hide toolbar and fab on doc list scroll --- docs-android/app/src/main/AndroidManifest.xml | 4 ++ .../sismics/docs/activity/MainActivity.java | 24 ++++++++-- .../docs/fragment/DocListFragment.java | 22 ++------- .../sismics/docs/ui/ScrollingFABBehavior.java | 47 +++++++++++++++++++ .../src/main/res/layout/doc_list_fragment.xml | 14 ------ .../app/src/main/res/layout/main_activity.xml | 44 +++++++++++++++-- .../app/src/main/res/values/styles.xml | 8 ++++ 7 files changed, 121 insertions(+), 42 deletions(-) create mode 100644 docs-android/app/src/main/java/com/sismics/docs/ui/ScrollingFABBehavior.java diff --git a/docs-android/app/src/main/AndroidManifest.xml b/docs-android/app/src/main/AndroidManifest.xml index b56057b5..66708a1d 100644 --- a/docs-android/app/src/main/AndroidManifest.xml +++ b/docs-android/app/src/main/AndroidManifest.xml @@ -28,6 +28,7 @@ android:name=".activity.MainActivity" android:label="@string/app_name" android:launchMode="singleTop" + android:theme="@style/AppTheme.NoActionBar" android:windowSoftInputMode="adjustNothing"> @@ -43,6 +44,9 @@ + + + { + private int toolbarHeight; + + public ScrollingFABBehavior(Context context, AttributeSet attrs) { + super(context, attrs); + this.toolbarHeight = getToolbarHeight(context); + } + + @Override + public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull FloatingActionButton fab, @NonNull View dependency) { + return dependency instanceof AppBarLayout; + } + + @Override + public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull FloatingActionButton fab, @NonNull View dependency) { + if (dependency instanceof AppBarLayout) { + CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams(); + int fabBottomMargin = lp.bottomMargin; + int distanceToScroll = fab.getHeight() + fabBottomMargin; + float ratio = dependency.getY() /(float) toolbarHeight; + fab.setTranslationY(- distanceToScroll * ratio); + } + return true; + } + + private int getToolbarHeight(Context context) { + final TypedArray styledAttributes = context.getTheme().obtainStyledAttributes( + new int[] { R.attr.actionBarSize }); + int toolbarHeight = (int) styledAttributes.getDimension(0, 0); + styledAttributes.recycle(); + + return toolbarHeight; + } +} \ No newline at end of file diff --git a/docs-android/app/src/main/res/layout/doc_list_fragment.xml b/docs-android/app/src/main/res/layout/doc_list_fragment.xml index ff83398e..979a1672 100644 --- a/docs-android/app/src/main/res/layout/doc_list_fragment.xml +++ b/docs-android/app/src/main/res/layout/doc_list_fragment.xml @@ -1,6 +1,5 @@ @@ -37,17 +36,4 @@ android:textSize="16sp" android:layout_centerInParent="true"/> - - \ No newline at end of file diff --git a/docs-android/app/src/main/res/layout/main_activity.xml b/docs-android/app/src/main/res/layout/main_activity.xml index cc9d2fdc..e5375a54 100644 --- a/docs-android/app/src/main/res/layout/main_activity.xml +++ b/docs-android/app/src/main/res/layout/main_activity.xml @@ -6,11 +6,47 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - + android:layout_height="match_parent"> + + + + + + + + + + + + + @color/colorAccent + +